Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7771300
  • 博文数量: 701
  • 博客积分: 2150
  • 博客等级: 上尉
  • 技术积分: 13233
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-29 16:28
个人简介

天行健,君子以自强不息!

文章分类

全部博文(701)

文章存档

2019年(2)

2018年(12)

2017年(76)

2016年(120)

2015年(178)

2014年(129)

2013年(123)

2012年(61)

分类: Python/Ruby

2012-02-14 21:12:09

使用Perl 发送邮件有很多方式,随便可数出来的有:
mail,
sendmail,
Mail::Mailer,
MIME::Lite
等等。这些方式都能实现邮件的发送,具体的就要看用在什么场合了。
1. mail方式
这个调用系统命令,算是最简单的方式了(我亲自测试可用)
  1. my $mail_title= "test_mail";
  2. my $mail_body = "ffmpeg error";
  3. my $mail_to = '';
  4. my $cmd_mail = "echo $mail_body\|mail -s $mail_title -cb $mail_to";
  5. system($cmd_mail);
2. Mail::Mailer方式
  1. #!/usr/bin/perl
  2. use Mail::Mailer;

  3. my $from_address = '';
  4. my $to_address = '>';
  5. my $subject = "mail title";
  6. my $mail_body = "hello world!";

  7. my $mailer = Mail::Mailer->new("sendmail");
  8. my $mailer->open( { From => $from_address,
  9.                     To =>   $to_address,
  10.                     Subject => $subject,
  11.                   })or die ("Can't open: $!\n");
  12. print $mailer $mail_body;
  13. $mailer->close();

3. MIME::Lite方式
一般邮件发送(我亲自测试通过).
  1. #!/usr/bin/perl

  2. use MIME::Lite;
  3. use MIME::Words qw(encode_mimewords);

  4. my $subject = encode_mimewords("test mail",'Charset','GB2312');
  5. my $data ="test";
  6. my $to_address = '';

  7. my $msg = MIME::Lite->new (
  8.           From => 'root@localhost',
  9.           To => $to_address,
  10.           Subject => $subject,
  11.           Type => 'text/html',
  12.           Data => $data,
  13.           Encoding => 'base64',
  14.           ) or die "create container failed: $!";
  15. $msg->attr('content-type.charset' => 'GB2312');
  16. $msg->send('smtp','localhost',Debug=>0);
如果上述程序遇到下述的出错提示:
  1. SMTP mail() command failed:
  2. 5.5.4 <root@localhost>... Real domain name required for sender address
则需要检查你主机名,
  1. #cat /etc/hosts
并将“root@localhost”替换成“root@hostname”

另外 ,还可以使用MIME::Lite来发送中文HTML邮件,防止被ESP当作垃圾邮件干掉。
(摘自)
  1. use MIME::Lite;
  2. use MIME::Words qw(encode_mimewords);

  3. sub send_email {

  4.     my $self = shift;
  5.     my $to_address = shift;

  6.     my $subject = encode_mimewords("这里是中文标题",'Charset','GB2312');
  7.     my $data =<<EOF;
  8. <body>
  9. <p>这里是中文HTML内容。</p>
  10. </body>
  11. EOF

  12.     my $msg = MIME::Lite->new (
  13.         From => '',
  14.         To => $to_address,
  15.         Subject => $subject,
  16.         Type => 'text/html',
  17.         Data => $data,
  18.         Encoding => 'base64',
  19.     ) or die "create container failed: $!";

  20.     $msg->attr('content-type.charset' => 'GB2312');
  21.     $msg->send('smtp','localhost',Debug=>0);
  22. }
几个常识点:
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 => '',那么基本发不出去。
为什么?因为126.com设置了SPF,接收方MTA多半会验证这个SPF,你的IP当然不在126的SPF里,所以通不过验证。
h. $data变量包含的是信件body的HTML编码,这个body里不要有很多链接、图片之类,否则容易被Spamassassin之类的反垃圾软件干掉。
i. 最后,控制发送频率,大量的发送会引起各个反垃圾系统的警惕,并将你列入黑名单

4. Mail::Sendmail方式


阅读(8678) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~