PerlCookbook2介绍$msg=MIME::Lite->new('X-Song-Playing:'=>'NatchezTrace');然而,当参数名代表的邮件头在表18-2中时,后面可以不加冒号。下表中*代表通配符,例如Content-*可以代表Content-Type和Content-ID但是不代表Dis-Content 表18-2: Approved | Encrypted | Received | Sender | Bcc | From | References | Subject | Cc | Keywords | Reply-To | To | Comments | Message-ID | Resent- | X- | Content-* | Return-Path | Date | Organization | |
表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);如果你想创建多个MIME::Lite->send("smtp","mail.example.com");$msg=MIME::Lite->new(opts);#...$msg->send();#sendsusingSMTP发送邮件不是
$text=$msg->as_string;print方法可以把消息的字符串形式写入一个文件句柄自定的文件中:
$msg->print($SOME_FILEHANDLE);例子18-3是一个发送邮件的程序,它把在命令行输入的文件名作为附件例18-3:发送带附件的邮件
#!/usr/bin/perl-w#mail-attachment-sendfilesasattachmentsuseMIME::Lite;useGetopt::Std;my$SMTP_SERVER='smtp.example.com';#可根据自己情况改变my$DEFAULT_SENDER='sender@example.com';#同上my$DEFAULT_RECIPIENT='recipient@example.com';#同上MIME::Lite->send('smtp',$SMTP_SERVER,Timeout=>60);my(o,$msg);#processoptionsgetopts('hf:t:s:',\o);$o{f}||=$DEFAULT_SENDER;$o{t}||=$DEFAULT_RECIPIENT;$o{s}||='Yourbinaryfile,sir';if($o{h}or!@ARGV){die"usage:\n\t$0[-h][-ffrom][-tto][-ssubject]file...\n";}#constructandsendemail$msg=newMIME::Lite(From=>$o{f},To=>$o{t},Subject=>$o{s},Data=>"Hi",Type=>"multipart/mixed",);while(@ARGV){$msg->attach('Type'=>'application/octet-stream','Encoding'=>'base64','Path'=>shift@ARGV);}$msg->send();