如何在 perl 中发送 HTML/Plain 文本邮件
how to send HTML/Plain Text mail in perl
我是 perl 语言的新手,我想在同一封电子邮件中发送纯文本文件的内容和 html 文本。我在哪里获取文本文件的文件内容,但我的 HTML 文本不起作用,即我的电子邮件中不是粗体句子。谁能解释一下我的 html 标签是如何工作的。下面是我的完整代码。
P.S:当我删除 print MAIL "MIME-Version: 1.0" 行时,我的 html 标签有效但文本文件无效(不逐行打印)。
use MIME::Lite;
my $HOME ='/apps/stephen/data';
my $FILE ="$HOME/LOG.txt";
my @HTML =();
push(@HTML,"<b>To send the content of a file in email</b><br>");
push(@HTML,`cat $FILE`);
&sendMail;
sub sendMail
{
$sub="TEST";
$from='ABC@ABC.com';
$to='ABC@ABC.com';
open(MAIL, "|/usr/lib/sendmail -t");
print MAIL "From: $from "; print MAIL "To: $to ";print MAIL "Cc: $Cc ";
print MAIL "Subject: $sub ";
print MAIL "MIME-Version: 1.0" ;
print MAIL "Content-Type: text/html ";
print MAIL "Content-Disposition:inline ";
print MAIL @HTML;
close(MAIL);
}
这并不是 Perl 特有的。
如果您想发送一封邮件,其中包含相同数据的替代表示,您必须使用 multipart/alternative,即
Mime-Version: 1.0
Content-type: multipart/alternative; boundary=foobar
--foobar
Content-type: text/plain
Plain text here
--foobar
Content-type: text/html
<p>HTML text here</p>
--foobar--
这样邮件程序将选择最佳表示。由于手动构建此类邮件可能很困难,因此您最好使用 Email:MIME, MIME::Lite or MIME::tools.
等模块
P.S: when i remove the line print MAIL "MIME-Version: 1.0" my html tag works but text file is not working (does not prints line by line).
难怪你忘记了行尾,即而不是
print MAIL "MIME-Version: 1.0" ;
应该是
print MAIL "MIME-Version: 1.0\n" ;
除此之外,使用 \n
而不是
更清楚。
如果您坚持手工创建 MIME-Message,请仔细查看相关标准,另请参阅 http://en.wikipedia.org/wiki/MIME。值得注意的是 Mail/Mime header 和 body 之间必须有一个空行,并且不需要在每个 header 行的末尾添加 space。
您正准备 use MIME::Lite
但后来您忘记了这一切并尝试手动拼凑 MIME 结构。即使您确切地知道自己在做什么,那也是痛苦且容易出错的;您绝对应该改用一组合适的库函数,以保持您的代码简单易读,并专注于实际任务。
MIME::Lite
documentation 在简介的第二个示例中展示了如何执行此操作。
已适应您的存根代码,
use MIME::Lite;
use strict; # always
use warnings; # always
### Create a new multipart message:
$msg = MIME::Lite->new(
From => 'ABC@ABC.com',
To => 'ABC@ABC.com',
#Cc => 'some@other.com, some@more.com',
Subject => 'TEST your blood pressure with some CAPS LOCK torture',
Type => 'multipart/mixed'
);
### Add parts (each "attach" has same arguments as "new"):
$msg->attach(
Type => 'text/html',
Data => join ('\n', '<b>To see the content of a file in email</b><br/>',
'<strong><blink><a href="cid:LOG.txt">click here</a></blink></strong>')
);
$msg->attach(
Type => 'text/plain',
Path => '/apps/stephen/data/LOG.txt',
Filename => 'LOG.txt',
Disposition => 'attachment'
);
$msg->send();
我是 perl 语言的新手,我想在同一封电子邮件中发送纯文本文件的内容和 html 文本。我在哪里获取文本文件的文件内容,但我的 HTML 文本不起作用,即我的电子邮件中不是粗体句子。谁能解释一下我的 html 标签是如何工作的。下面是我的完整代码。 P.S:当我删除 print MAIL "MIME-Version: 1.0" 行时,我的 html 标签有效但文本文件无效(不逐行打印)。
use MIME::Lite;
my $HOME ='/apps/stephen/data';
my $FILE ="$HOME/LOG.txt";
my @HTML =();
push(@HTML,"<b>To send the content of a file in email</b><br>");
push(@HTML,`cat $FILE`);
&sendMail;
sub sendMail
{
$sub="TEST";
$from='ABC@ABC.com';
$to='ABC@ABC.com';
open(MAIL, "|/usr/lib/sendmail -t");
print MAIL "From: $from "; print MAIL "To: $to ";print MAIL "Cc: $Cc ";
print MAIL "Subject: $sub ";
print MAIL "MIME-Version: 1.0" ;
print MAIL "Content-Type: text/html ";
print MAIL "Content-Disposition:inline ";
print MAIL @HTML;
close(MAIL);
}
这并不是 Perl 特有的。 如果您想发送一封邮件,其中包含相同数据的替代表示,您必须使用 multipart/alternative,即
Mime-Version: 1.0
Content-type: multipart/alternative; boundary=foobar
--foobar
Content-type: text/plain
Plain text here
--foobar
Content-type: text/html
<p>HTML text here</p>
--foobar--
这样邮件程序将选择最佳表示。由于手动构建此类邮件可能很困难,因此您最好使用 Email:MIME, MIME::Lite or MIME::tools.
等模块P.S: when i remove the line print MAIL "MIME-Version: 1.0" my html tag works but text file is not working (does not prints line by line).
难怪你忘记了行尾,即而不是
print MAIL "MIME-Version: 1.0" ;
应该是
print MAIL "MIME-Version: 1.0\n" ;
除此之外,使用 \n
而不是 更清楚。
如果您坚持手工创建 MIME-Message,请仔细查看相关标准,另请参阅 http://en.wikipedia.org/wiki/MIME。值得注意的是 Mail/Mime header 和 body 之间必须有一个空行,并且不需要在每个 header 行的末尾添加 space。
您正准备 use MIME::Lite
但后来您忘记了这一切并尝试手动拼凑 MIME 结构。即使您确切地知道自己在做什么,那也是痛苦且容易出错的;您绝对应该改用一组合适的库函数,以保持您的代码简单易读,并专注于实际任务。
MIME::Lite
documentation 在简介的第二个示例中展示了如何执行此操作。
已适应您的存根代码,
use MIME::Lite;
use strict; # always
use warnings; # always
### Create a new multipart message:
$msg = MIME::Lite->new(
From => 'ABC@ABC.com',
To => 'ABC@ABC.com',
#Cc => 'some@other.com, some@more.com',
Subject => 'TEST your blood pressure with some CAPS LOCK torture',
Type => 'multipart/mixed'
);
### Add parts (each "attach" has same arguments as "new"):
$msg->attach(
Type => 'text/html',
Data => join ('\n', '<b>To see the content of a file in email</b><br/>',
'<strong><blink><a href="cid:LOG.txt">click here</a></blink></strong>')
);
$msg->attach(
Type => 'text/plain',
Path => '/apps/stephen/data/LOG.txt',
Filename => 'LOG.txt',
Disposition => 'attachment'
);
$msg->send();