本文共 2890 字,大约阅读时间需要 9 分钟。
MIME::Lite发邮件附件(并有结合Net::SMTP::TLS进行账号认证):
# cpan MIME::Lite
# cpan Net::SMTP::TLS
To多人格式:
$email{'To'}='<zhi.yang@ming.com>,<ming.yang@ming.com>';
方式一:
注:
1、该方式是将指定路径文件以附件形式发送
2、Data处必须有,内容可自己调整
3、$msg->attach需写两处,也可再写$msg->attach添加多个附件(后面的第1部分就不用重复写了)
# vi mail.pl
#!/usr/bin/perl -w
use MIME::Lite;
use Net::SMTP::TLS;
my %email;
$email{'To'}='zhi.yang@ming.com';
$email{'Cc'}='zhi.yang@ming.com';
$email{'From'}= 'zhi.yang@ming.com';
$email{'SmtpHost'}= 'SMTP.ming.com';
$email{'SmtpPort'}= '587';
$email{'Hello'}= 'ming.com';
$email{'User'}= 'zhi.yang';
$email{'Password'}= 'xxxx';
my $msg=MIME::Lite->new(
From=> $email{'From'},
To=> $email{'To'},
Cc=>$email{'Cc'},
Subject=>$email{'Subject'},
Type => 'multipart/mixed'
);
$msg->attach(
Type => 'TEXT',
Data => 'aa',
);
$msg->attach(
Type => 'TEXT',
Path => '/root/aa.log',
Disposition => 'attachment'
);
my $smtp = Net::SMTP::TLS->new(
"$email{'SmtpHost'}:$email{'SmtpPort'}",
User => $email{'User'},
Password => $email{'Password'} ,
Hello => $email{'Hello'},
Timeout => 30
) or die("Failure to connect mail host!\n");
my $contentString = $msg->as_string() or die "$!";
$smtp->mail($email{'From'});
$smtp->to($email{'To'});
$smtp->data();
$smtp->datasend($contentString);
$smtp->dataend();
$smtp->quit();
:wq
方式二:
注:
1、Disposition => 'attachment' 表示以附件显示,inline表示在邮件正文中显示
2、邮件内容采用的HTML格式,<dt>表示坐端开头写,<dd>表示左端空2个字符写,<br>表空一行
3、cc处可写多个人,中间用","隔开,To处貌似只能写1个
# vi mail.pl
#!/usr/bin/perl -w
use MIME::Lite;
use Net::SMTP::TLS;
my %email;
$email{'To'}='zhi.yang@ming.com';
$email{'Cc'}='zhi.yang@ming.com';
$email{'From'}= 'zhi.yang@ming.com';
$email{'SmtpHost'}= 'SMTP.ming.com';
$email{'SmtpPort'}= '587';
$email{'Hello'}= 'ming.com';
$email{'User'}= 'zhi.yang';
$email{'Password'}= 'xxxxx';
&mailWithAttachment_send(%email);
sub mailWithAttachment_send(){
my $msg=MIME::Lite->new(
From=> $email{'From'},
To=> $email{'To'},
Cc=>$email{'Cc'},
Subject=>$email{'Subject'},
Type => 'multipart/mixed'
);
$msg->attach(
Type => 'TEXT/HTML',
Encoding => 'quoted-printable',
Data => qq{
<dl>
<dt>Dear all:</dt>
<dd>CTS测试报告已出,</dd>
<dd>详见数据平台:</dd>
<dd><a class="text-justify" href="http://aa.ming.com/bb.html">http://aa.ming.com/bb.html</a></dd>
<dd><br></dd>
<dt>Dear</dt>
<dd>请关注测试结果并将差异/修改引入问题提交CR,多谢~~~</dd>
<dd><br></dd>
<dd>其中数据平台Log</dd>
<dd><br></dd>
<dd>Thanks!</dd>
</dl>
},
Disposition => 'inline' (inline表示显示在邮件正文中,attachment表示以附件显示)
);
my $smtp = Net::SMTP::TLS->new(
"$email{'SmtpHost'}:$email{'SmtpPort'}",
User => $email{'User'},
Password => $email{'Password'} ,
Hello => $email{'Hello'},
Timeout => 30
) or die("Failure to connect mail host!\n");
my $contentString = $msg->as_string() or die "$!";
$smtp->mail($email{'From'});
$smtp->to($email{'To'});
$smtp->data();
$smtp->datasend($contentString);
$smtp->dataend();
$smtp->quit();
}
:wq