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&开发者_如何学编程gt;</head><body>@emailBody";
close(MAIL)
Is that the correct way of doing it? It is not working for some reason. Thanks for your help.
Start with Email::Sender::Simple or Email::Sender.
There is a quickstart guide in CPAN, and Ricardo wrote a good use-me in his 2009 advent calendar
From the quickstart guide:
use strict;
use Email::Sender::Simple qw(sendmail);
use Email::Simple;
use Email::Simple::Creator;
my $email = Email::Simple->create(
header => [
To => '"Xavier Q. Ample" <x.ample@example.com>',
From => '"Bob Fishman" <orz@example.mil>',
Subject => "don't forget to *enjoy the sauce*",
'Content-Type' => 'text/html',
],
body => "<p>This message is short, but at least it's cheap.</p>",
);
sendmail($email);
The content type should be part of the mail header. Right now it's part of the mail body. The header is separated from the body by a double newline. So, removing the second newline after the subject header should fix the problem of content type not being correctly interpreted.
You should not really talk to sendmail directly via a pipe. Instead use a proper CPAN module.
Email::Sender is an example.
Mail::Sender has a specific guide on sending HTML messages
If you are just generating spewy emails and you don't need massive robustness or tweaking, you could always just take the shortcut way...
use Email::Stuff;
my $html = <<'END_HTML';
<html>
...
</html>
END_HTML
Email::Stuff->to('"Xavier Q. Ample" <x.ample@example.com>')
->from('"Bob Fishman" <orz@example.mil>')
->subject("Don't forget to *enjoy the sauce*")
->html_body($body)
->send;
Using html tag "pre" will be a simple way to send the script
output in HTML email.
open(MAIL, "|/usr/sbin/sendmail -t");
print MAIL "To: $EMAIL\n";
print MAIL "From: $FROM\n";
print MAIL "Subject: $SUBJECT";
print MAIL "Content-Type: text/html; charset=ISO-8859-1\n\n";
print MAIL < pre >\n$mailoutput< /pre >\n;
close(MAIL);
That will allow you to do all the formating in your script and will
get the same output in email as on screen. [ as you know make sure
no space before and after "pre" ]
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.
You can use Email::MIME
my $message = Email::MIME->create(
header_str => [
From => 'no-reply@example.com',
To => $address,
Subject => encode_mimewords($subject,
Charset => 'utf-8', Encoding => 'B'),
'Content-Type' => 'text/html',
],
attributes => {
encoding => 'base64',
charset => 'UTF-8',
},
body_str => $message_body,
);
sendmail($message);
精彩评论