使用Perl 发送邮件有很多方式,随便可数出来的有:
mail,
sendmail,
Mail::Mailer,
MIME::Lite
等等。这些方式都能实现邮件的发送,具体的就要看用在什么场合了。
1. mail方式
这个调用系统命令,算是最简单的方式了(我亲自测试可用)
2. Mail::Mailer方式
3. MIME::Lite方式
一般邮件发送(我亲自测试通过).
另外 ,还可以使用MIME::Lite来发送中文HTML邮件,防止被ESP当作垃圾邮件干掉。
(摘自http://bbs.chinaunix.net/thread-1855349-1-1.html)
几个常识点:
a. 标题必须用MIME::Words编码,很多人忽略了这点。
b. MIME::Lite构造信件时,Type不要搞错。例如只是一封HTML邮件,没有附件之类,Type就是text/html。
21CN的webmail发信不管有没有附件,Type都是multipart/mixed,结果被Gmail直接扔进垃圾箱。
c. 信件要选择传输编码(Encoding),常用的是base64和quoted-printable,我推荐base64。
d. 信件body的content-type charset要设置正确,例如中文GB2312。
e. 最后一句$msg->send('smtp','localhost',Debug=>0)调用Net::SMTP发信,本机安装了MTA例如Postfix就可以。这个发信IP最好是信誉比较好的IP,没有列入sorbs、spamcop、spamhaus等RBL列表里。
f. 发信IP最好有反向解析(PTR),否则肯定发不到AOL之类的验证反解的邮箱。
g. 那个From地址也最好真实存在,但是不要用知名网站的免费邮箱,例如From => 'abc@126.com',那么基本发不出去。
为什么?因为126.com设置了SPF,接收方MTA多半会验证这个SPF,你的IP当然不在126的SPF里,所以通不过验证。
h. $data变量包含的是信件body的HTML编码,这个body里不要有很多链接、图片之类,否则容易被Spamassassin之类的反垃圾软件干掉。
i. 最后,控制发送频率,大量的发送会引起各个反垃圾系统的警惕,并将你列入黑名单
4. Mail::Sendmail方式
mail,
sendmail,
Mail::Mailer,
MIME::Lite
等等。这些方式都能实现邮件的发送,具体的就要看用在什么场合了。
1. mail方式
这个调用系统命令,算是最简单的方式了(我亲自测试可用)
- my $mail_title= "test_mail";
- my $mail_body = "ffmpeg error";
- my $mail_to = 'xxxxxx@qq.com';
- my $cmd_mail = "echo $mail_body\|mail -s $mail_title -cb $mail_to";
- system($cmd_mail);
- #!/usr/bin/perl
- use Mail::Mailer;
- my $from_address = '<root@localhost>';
- my $to_address = 'xxxxxxxx@qq.com>';
- my $subject = "mail title";
- my $mail_body = "hello world!";
- my $mailer = Mail::Mailer->new("sendmail");
- my $mailer->open( { From => $from_address,
- To => $to_address,
- Subject => $subject,
- })or die ("Can't open: $!\n");
- print $mailer $mail_body;
- $mailer->close();
3. MIME::Lite方式
一般邮件发送(我亲自测试通过).
- #!/usr/bin/perl
- use MIME::Lite;
- use MIME::Words qw(encode_mimewords);
- my $subject = encode_mimewords("test mail",'Charset','GB2312');
- my $data ="test";
- my $to_address = 'xxxxxxx@qq.com';
- my $msg = MIME::Lite->new (
- From => 'root@localhost',
- To => $to_address,
- Subject => $subject,
- Type => 'text/html',
- Data => $data,
- Encoding => 'base64',
- ) or die "create container failed: $!";
- $msg->attr('content-type.charset' => 'GB2312');
- $msg->send('smtp','localhost',Debug=>0);
如果上述程序遇到下述的出错提示:
- SMTP mail() command failed:
- 5.5.4 <root@localhost>... Real domain name required for sender address
则需要检查你主机名,
- #cat /etc/hosts
并将“root@localhost”替换成“root@hostname”
(摘自http://bbs.chinaunix.net/thread-1855349-1-1.html)
- use MIME::Lite;
- use MIME::Words qw(encode_mimewords);
- sub send_email {
- my $self = shift;
- my $to_address = shift;
- my $subject = encode_mimewords("这里是中文标题",'Charset','GB2312');
- my $data =<<EOF;
- <body>
- <p>这里是中文HTML内容。</p>
- </body>
- EOF
- my $msg = MIME::Lite->new (
- From => 'you@example.com',
- To => $to_address,
- Subject => $subject,
- Type => 'text/html',
- Data => $data,
- Encoding => 'base64',
- ) or die "create container failed: $!";
- $msg->attr('content-type.charset' => 'GB2312');
- $msg->send('smtp','localhost',Debug=>0);
- }
a. 标题必须用MIME::Words编码,很多人忽略了这点。
b. MIME::Lite构造信件时,Type不要搞错。例如只是一封HTML邮件,没有附件之类,Type就是text/html。
21CN的webmail发信不管有没有附件,Type都是multipart/mixed,结果被Gmail直接扔进垃圾箱。
c. 信件要选择传输编码(Encoding),常用的是base64和quoted-printable,我推荐base64。
d. 信件body的content-type charset要设置正确,例如中文GB2312。
e. 最后一句$msg->send('smtp','localhost',Debug=>0)调用Net::SMTP发信,本机安装了MTA例如Postfix就可以。这个发信IP最好是信誉比较好的IP,没有列入sorbs、spamcop、spamhaus等RBL列表里。
f. 发信IP最好有反向解析(PTR),否则肯定发不到AOL之类的验证反解的邮箱。
g. 那个From地址也最好真实存在,但是不要用知名网站的免费邮箱,例如From => 'abc@126.com',那么基本发不出去。
为什么?因为126.com设置了SPF,接收方MTA多半会验证这个SPF,你的IP当然不在126的SPF里,所以通不过验证。
h. $data变量包含的是信件body的HTML编码,这个body里不要有很多链接、图片之类,否则容易被Spamassassin之类的反垃圾软件干掉。
i. 最后,控制发送频率,大量的发送会引起各个反垃圾系统的警惕,并将你列入黑名单
4. Mail::Sendmail方式