如何使用 Perl 发送 HTML 电子邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2623651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I send an HTML email with Perl?
提问by AlexBrand
I am trying to send an HTML email using Perl.
我正在尝试使用 Perl 发送 HTML 电子邮件。
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.
这是正确的做法吗?由于某种原因它不起作用。谢谢你的帮助。
采纳答案by BalusC
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.
内容类型应该是邮件标题的一部分。现在它是邮件正文的一部分。标题与正文由双换行符分隔。因此,删除主题标题后的第二个换行符应该可以解决内容类型未被正确解释的问题。
回答by spazm
Start with Email::Sender::Simpleor Email::Sender.
There is a quickstart guide in CPAN, and Ricardo wrote a good use-mein his 2009 advent calendar
从Email::Sender::Simple或Email::Sender 开始。CPAN 中
有一个快速入门指南,Ricardo在他的 2009 advent calendar 中写了一个很好的use-me
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" <[email protected]>',
From => '"Bob Fishman" <[email protected]>',
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);
回答by DVK
You should not really talk to sendmail directly via a pipe. Instead use a proper CPAN module.
您不应该真正通过管道直接与 sendmail 交谈。而是使用适当的 CPAN 模块。
Email::Senderis an example.
Email::Sender就是一个例子。
回答by Adam Kennedy
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" <[email protected]>')
->from('"Bob Fishman" <[email protected]>')
->subject("Don't forget to *enjoy the sauce*")
->html_body($body)
->send;
回答by Robin
Using html tag "pre" will be a simple way to send the script
output in HTML email.
使用 html 标签“pre”将是一种
在 HTML 电子邮件中发送脚本输出的简单方法。
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" ]
这将允许您在脚本中进行所有格式化,并
在电子邮件中获得与屏幕上相同的输出。[如您所知,请确保
“pre”前后没有空格]
回答by skohler
I had a problem when sending a MIME multipart message from Perl using sendmail.
使用 sendmail 从 Perl 发送 MIME 多部分消息时遇到问题。
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:
经过几个小时的挫折后,我发现整个消息需要在一个变量中,并带有单个语句才能将消息发送到 sendmail。因此,例如,如果您的消息完全包含在名为 $email_msg 的变量中,则通过 sendmail 发送消息将如下所示:
$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.
这是在 CentOS 服务器上使用 Perl 5.8.8。
回答by Petru
You can use Email::MIME
您可以使用电子邮件::MIME
my $message = Email::MIME->create(
header_str => [
From => '[email protected]',
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);