v.coder v.coder - 1 year ago 73
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

, To address is
and subject is
"test subject"
. I get the email with
field as

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;

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

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

Answer Source

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";

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download