v.coder v.coder - 2 months ago 11
Perl Question

Sending email using perl with sendmail

I am trying to send email using sendmail in perl. Email is sent but the content which I send as Subject gets added to 'To:' in the email. For example, if from address is

from@gmail.com
, To address is
to@gmail.com
and subject is
"test subject"
. I get the email with
Reply-to:
field as
from@gmail.com,"Subject:test.email","To:to"@gmail.com,"Content-type:text/plain"


Here is my code:

open(SENDMAIL, "|/usr/lib/sendmail -oi -t '$to_email' -f '$from_email'") || ($error_message .= "<P>Unable to open email process.</P>");
print SENDMAIL $reply_to;
print SENDMAIL $subject;
print SENDMAIL $send_to;
print SENDMAIL "Content-type: text/plain\n\n";
print SENDMAIL $content;
close(SENDMAIL);


And if I remove $reply_to and $send_to lines, the email comes with
from:
field
as Apache server.

Any help would be appreciated. I do not want to use any other library like
Email::MIME
since it doesn't exist on my system.

Answer

I am not sure what is wrong with what you show, but here is some working code

my ($header, $From, $To, $Cc, $ReplyTo, $Subject);

$From    = "From: $from";
$To      = "To: $to";
$Cc      = "Cc: $cc_addresses";
$ReplyTo = "Reply-To: $replyto";
$Subject = "Subject: $subj";

# Form the header.  The fields always submitted:
$header = join("\n", $From, $To, $ReplyTo, $Subject, '');

# Optional fields
$header .= "$Cc\n" if $cc_addresses;

open(SENDMAIL, "|/usr/sbin/sendmail -oi -t")
    or carp "Can't fork for sendmail: $!\n";
say SENDMAIL "$header\n$msg_cont";
close(SENDMAIL);

Note. There is my $cc_addresses = ''; earlier, which then (possibly) gets built up.