Alex Rivera | Logout

How can I send an HTML email with Perl?

Asked 2010-04-12T16:32:35.793
12

I am trying to send an HTML email using Perl.

 open(MAIL,"|/usr/sbin/sendmail -t");

    ## Mail Header
    print MAIL "To: $to\n";
    print MAIL "From: $from\n";
    print MAIL "Subject: $subject\n\n";
    ## Mail Body
    print MAIL "Content-Type: text/html; charset=ISO-8859-1\n\n"
        . "<html><head></head><body>@emailBody";
 close(MAIL)

Is that the correct way of doing it? It is not working for some reason. Thanks for your help.

Edit
Report

1 Answer

1

I had a problem when sending a MIME multipart message from Perl using sendmail.

After a couple several hours of frustration I found that the entire message needed to be in a variable with at single statement to send the message to sendmail. So for example, if your message is contained completely in a variable called $email_msg, sending the message through sendmail would look like:

$mailprog = '/usr/sbin/sendmail';
open(MAIL,"|$mailprog -t");
print MAIL $email_msg;
close MAIL;

This works, while using many "print MAIL "message contents"" does not seem to send a mail message that some mail readers can handle as expected.

This is using Perl 5.8.8 on a CentOS server.

answered 2013-03-31T18:46:24.123

Your Answer