|
发现N久没写博文了,有什么笔记也直接记到evernote里了,也不知这是好习惯还是坏习惯鸟~
记得在哪看过说,perl语言精髓是达到一个目的可以有很多种方法。
这话说得挺对的,因为就光发送邮件,你也会发现TM方法太多了,还得google一下选个比较好的方法
最后选了使用MIME::Lite模块,感觉挺强大的,上手也快
代码如下
sub mail_list{
exit 99 if(!@some_rule);
my ($sec,$min,$hour,$day,$month,$year,@tmp)=localtime; # 邮件加个时间戳
($min,$hour,$day,$month,$year) = (
sprintf("%02d",$min),
sprintf("%02d",$hour),
sprintf("%02d",$day),
sprintf("%02d",++$month),
sprintf("%04d",$year+1900)
);
my $from_addr = 'root@xxx.com'; #发送方
my $to_addr = 'flw521521@xxx.com'; #接收方
my $cc_addr = 'cc@xxx.com'; #抄送
my $subject = '邮件标题'; #邮件标题
my $encode = 'quoted-printable'; #模块发送邮件内容时采用的编码方式
my $data = "say hello\n";
my $msg = MIME::Lite->new(
From => $from_addr,
To => $to_addr,
Cc => $cc_addr,
Subject => $subject,
Type => 'multipart/mixed' # 指定总的内容为多种,如果要发送附件的话,就是这个模式
);
$msg->attach(
Type => 'text/html;charset=gb2312', #指定邮件内容编码方式
Encoding => $encode,
Data => qq{ #接下来就是邮件中文了,简单的html编码,其中可以穿插perl变量
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>
<boby>
<h5>您好!</h5>
<table border="1">
<tr>
<th>表格1</th><th>表格2</th><th>表格3</th>
</tr>
</table>
<hr />
$data #穿插一个变量,很方便
<hr />
<h5 align="right">ITEYE</h5>
<h5 align="right">flw521521</h5>
<h5 align="right">$year-$month-$day $hour:$min</h5>
</boby>
</html>
},
);
$msg->send;
}
编码这块,有两个,一个是MIME::Lite模块发送内容时的编码,一个html的编码
html代码那块的编码head编码设定,可要可不要,保险起见,就写一下。
如果要发送其他附件的话,添加一些邮件附属信息即可
$msg->attach( //发送附件
Type => 'auto', //附件类型自动判断
Path => '/home/abc.txt', //附件的路径
Disposition => 'attachment', //配置为附件,不在正文内
);
$msg->attach( //发送图片附件,随正文
Type =>'image/gif',
Path =>'/home/abc.gif',
Filename =>'logo.gif',
);
就这些吧,具体模块用法转至CPAN:http://search.cpan.org/~rjbs/MIME-Lite-3.029/lib/MIME/Lite.pm |
|
|