PerlCookbook2介绍

作者:凯旋网络来源:凯旋网络
以把send作为类方法调用,此时默认发送方法会被替换。
MIME::Lite->send("smtp","mail.example.com");$msg=MIME::Lite->new(opts);#...$msg->send();#sendsusingSMTP

如果你要处理多个消息,用好ReadNow参数。它指定附件应该立即从文件或文件句柄中读取发送,而不是在发送前转化为字符串。

发送邮件不是MIME::Lite能做的唯一事情。你还可以用它把最后的邮件内容变成字符串:

$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();
  • 广告推荐