加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

在perl中使用 MIME::Lite来发送有附件的邮件

发布时间:2020-12-16 00:29:13 所属栏目:大数据 来源:网络整理
导读:?? 工作总需要通过linux系统自动发送邮件功能,来实现针对数据库和系统的监控,并定时发送警报到邮箱,所以使用到MIME::Lite 模块,不过个人对该功能理解不是很透彻,也仅仅只是使用,为更完整的写一篇博文,供大家参考,所以在网络上参考别人的blog并整理。

?? 工作总需要通过linux系统自动发送邮件功能,来实现针对数据库和系统的监控,并定时发送警报到邮箱,所以使用到MIME::Lite 模块,不过个人对该功能理解不是很透彻,也仅仅只是使用,为更完整的写一篇博文,供大家参考,所以在网络上参考别人的blog并整理。

首先,创建包含邮件各种头信息的 MIME::Lite 对象:

 
 
  1. use?MIME::Lite;?
  2. ??
  3. $msg?=?MIME::Lite->new(?
  4. From??=>?'sender@example.com',?
  5. To???=>?'recipient@example.com',?
  6. Subject?=>?'You?want?awstats??',?
  7. Type??=>?'multipart/mixed'?
  8. );?

然后用 attach 方法添加附件内容:

 
 
  1. $msg->attach(?
  2. Type????=>?'auto',?
  3. Path????=>?'/root/awstats.gz',?
  4. );?

MIME::Lite???? 模块的参数类型决定附件的类型和附件的添加方法:
Path???? 指定作为附件的文件的路径
Filename???? 指定接受方保存附件时,附件的默认文件名。如果指定了 Path 参数,那么默认的文件名就是路径中的名字
Data???? 指定附件添加的日期
Type???? 指定待添加附件的文件编码类型
Disposition??? 它的值只能是 inline 和 attachment。前者指定接受方打开邮件的时候附件内容会跟在邮件正文后显示,而不单独作为一个附加物。后者指定接受方应该指定一个附件的解码方 法,并且保存附件,此时会有提示

 
 
  1. $msg->send(?);??????#?默认的方法是用sendmail规则发送?
  2. $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 下载

使用方法

 
 
  1. 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 格式文件。

这里是我实际使用到脚本,供大家参考

 
 
  1. echo?@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@>>?/mnt/nas/monitor_log/all.log?
  2. echo?@@@@@@@@@@@@@@@@@@@@SERVER_NAME@@@@@@@@@@@@@@@@@@@@@@@@@@@@@>>?/mnt/nas/monitor_log/all.log?
  3. echo?@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@>>?/mnt/nas/monitor_log/all.log?
  4. cat?/mnt/nas/monitor_log/xxxx.log?>>?/mnt/nas/monitor_log/all.log?
  5. mydat=`date?+%Y%m%d%H%M`?
  6. cp?/mnt/nas/monitor_log/all.log??/mnt/nas/monitor_log/all_log_his/all_log_$mydat?
  7. ?
  8. #!/usr/bin/perl?-w?
  9. use?MIME::Lite;?
  10. ?
  11. $FROM='xxxx@163.com';?
  12. $TO='xxxx@163.com';?
  13. $subject='European?Database?Server?monitor';?
  14. $message_text=`cat?/mnt/nas/monitor_log/all.log`;?
  15. $msg?=?MIME::Lite->new(From?=>?$FROM,To?=>?$TO,Subject?=>?$subject,Type?=>?'multipart/mixed');?
  16. $msg->attach(Encoding?=>'quoted-printable',Type?=>'text/plain;charset=UTF-8',Data?=>?$message_text);?
  17. $msg->attach(Type?=>'text/plain;charset=UTF-8',Filename?=>?'monitor_log.txt',Path?=>?'/mnt/nas/monitor_log/all.log',?Disposition?=>?'attachment');?
  18. $msg->send('smtp','172.16.4.25',?Debug=>1?);?

?

?

参考转载自:扶凯

原文链接: http://www.php-oa.com/2009/12/24/perl-mimelite-attach.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读