?? 工作总需要通过linux系统自动发送邮件功能,来实现针对数据库和系统的监控,并定时发送警报到邮箱,所以使用到MIME::Lite 模块,不过个人对该功能理解不是很透彻,也仅仅只是使用,为更完整的写一篇博文,供大家参考,所以在网络上参考别人的blog并整理。
首先,创建包含邮件各种头信息的 MIME::Lite 对象:
- use?MIME::Lite;?
- ??
- $msg?=?MIME::Lite->new(?
- From??=>?'sender@example.com',?
- To???=>?'recipient@example.com',?
- Subject?=>?'You?want?awstats??',?
- Type??=>?'multipart/mixed'?
- );?
然后用 attach 方法添加附件内容:
- $msg->attach(?
- Type????=>?'auto',?
- Path????=>?'/root/awstats.gz',?
- );?
MIME::Lite???? 模块的参数类型决定附件的类型和附件的添加方法:
Path???? 指定作为附件的文件的路径
Filename???? 指定接受方保存附件时,附件的默认文件名。如果指定了 Path 参数,那么默认的文件名就是路径中的名字
Data???? 指定附件添加的日期
Type???? 指定待添加附件的文件编码类型
Disposition??? 它的值只能是 inline 和 attachment。前者指定接受方打开邮件的时候附件内容会跟在邮件正文后显示,而不单独作为一个附加物。后者指定接受方应该指定一个附件的解码方 法,并且保存附件,此时会有提示
- $msg->send(?);??????#?默认的方法是用sendmail规则发送?
- $msg->send('smtp',?'mailserver.example.com',?Timeout?=>?30,Debug?=?1);#?指定其它的方法?
使用MIME::Lite来发送邮件有二种方法 sendmail 和 Net::SMTP。
调用 send 方法时,若第一个参数为“smtp”,则用 Net::SMTP 发送邮件。send的其它参数都传给 Net::SMTP。不加任何的话就是系统的sendmail.
Perl mailSend 下载
使用方法
- echo?'aaaaaaaa'?|perl?mailSend.pl?--to?kai.fu?--subject?'you?subject'?--attach?awstats.gz?--attach?test.pl?
MIME::Lite 附录:
MIME::Lite 参数采用“参数名=>值”对形式。
MIME::Lite 头
Approved Encrypted Received Sender
Bcc From References Subject
Cc Keywords Reply-To To
Comments Message-ID Resent- X-
Content-* MIME-Version Return-Path
Date Organization
MIME::Lite 参数类型
Data FH ReadNow
Datestamp Filename Top
Disposition Id Type
Encoding Length
Filename Path
常用附件编码类型
TEXT?????????????? ? ? ? ? ?? 代表 text/plain,为 Type 的默认值;
BINARY???????????? ? ? ? ? ?? 是 application/octet-stream 的缩写;
multipart/mixed??? ? ? ?? ? ? 表明邮件有附件;
application/msword??????????? 表明附件为微软的 Word 文档;
application/vnd.ms-excel????? 表明附件为微软的 Excel 文档;
application/pdf ? ??????????? 表明附件为 PDF 文档;
image/gif,image/jpeg,image/png????? 分别指定 GIF,JPEG,PNG 文件;
audio/mpeg??????????????????? 指定 MP3 格式文件;video/mpeg 指定 MPEG 格式影片;
video/quicktime??? ? ? ? ? ?? 指定Quicktime 格式文件。
这里是我实际使用到脚本,供大家参考。
- echo?@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@>>?/mnt/nas/monitor_log/all.log?
- echo?@@@@@@@@@@@@@@@@@@@@SERVER_NAME@@@@@@@@@@@@@@@@@@@@@@@@@@@@@>>?/mnt/nas/monitor_log/all.log?
- echo?@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@>>?/mnt/nas/monitor_log/all.log?
- cat?/mnt/nas/monitor_log/xxxx.log?>>?/mnt/nas/monitor_log/all.log?
- mydat=`date?+%Y%m%d%H%M`?
- cp?/mnt/nas/monitor_log/all.log??/mnt/nas/monitor_log/all_log_his/all_log_$mydat?
-
?
- #!/usr/bin/perl?-w?
- use?MIME::Lite;?
- ?
- $FROM='xxxx@163.com';?
- $TO='xxxx@163.com';?
- $subject='European?Database?Server?monitor';?
- $message_text=`cat?/mnt/nas/monitor_log/all.log`;?
- $msg?=?MIME::Lite->new(From?=>?$FROM,To?=>?$TO,Subject?=>?$subject,Type?=>?'multipart/mixed');?
- $msg->attach(Encoding?=>'quoted-printable',Type?=>'text/plain;charset=UTF-8',Data?=>?$message_text);?
- $msg->attach(Type?=>'text/plain;charset=UTF-8',Filename?=>?'monitor_log.txt',Path?=>?'/mnt/nas/monitor_log/all.log',?Disposition?=>?'attachment');?
- $msg->send('smtp','172.16.4.25',?Debug=>1?);?
?
?
参考转载自:扶凯
原文链接: http://www.php-oa.com/2009/12/24/perl-mimelite-attach.html