摘要:发送邮件的时候添加附件
问题:你想要发一封包含附件的邮件,比如包含一份PDF格式的文档
解决:用CPAN上的MIME::Lite模块。
首先,创建包含邮件各种头信息的useMIME::Lite;$msg=MIME::Lite->new(From=>''sender@example.com'',To=>''recipient@example.com'',Subject=>''Myphotoforthebrochure'',Type=>''multipart/mixed'');然后用attach方法添加附件内容:
$msg->attach(Type=>''image/jpeg'',Path=>''/Users/gnat/Photoshopped/nat.jpg'',Filename=>''gnat-face.jpg'');
$msg->attach(Type=>''TEXT'',
Data=>''Ihopeyoucanusethis!'');最后,发送这份邮件,发送它的方法是可选的:
$msg->send();#默认的方法是用sendmail规则发送#指定其它的方法$msg->send(''smtp'',''mailserver.example.com'');
讨论:
$msg=MIME::Lite->new(''X-Song-Playing:''=>''NatchezTrace'');然而,当参数名代表的邮件头在表18-2中时,后面可以不加冒号。下表中*代表通配符,例如Content-*可以代表Content-Type和Content-ID但是不代表Dis-Content
表18-2:
表18-3:
这儿有几个有用的附件编码类型:TEXT代表text/plain,为Type的默认值;BINARY是application/octet-stream的缩写;multipart/mixed表明邮件有附件;application/msword表明附件为微软的Word文档;application/vnd.ms-excel表明附件为微软的Excel文档;application/pdf表明附件为
#timeoutof30seconds$msg->send("smtp","mail.example.com",Timeout=>30);如果你想创建多个