perl 发送邮件 可以发送附件
#cat send_mail.pl 另外一种实现方式 可以发送附件的 #*****************************************************************************##? Function Name:#?????? SendMail($$/@$$/@)#? Description:#?????? Use Net::SMTP and MIME::Lite to send Email with attachment.#? Argument:#?????? 1: Sending Email address#?????? 2: Password of sending Email.#?????? 3: The recipient Email array#?????? 4: The subject of the Email#?????? 5: The content of the Email#?????? 6: The attachments array of the Email#? Return:#?????? None#*****************************************************************************#sub SendMail($$/@$$/@){use Net::SMTP;use MIME::Lite;my ($mailFrom,$password,$mailToRef,$subject,$content,$attachmentRef)=@_;my ($userName,$mailHost) = split(//@/,$mailFrom);my $helloPara = $mailHost;# print "The Email user name is: $userName/n";# print "The mailHost is: $mailHost/n";print $mailHost;if ('163.com.cn' ne $mailHost){???? $mailHost = "smtp.".$mailHost;#???? print "The mailHost is: $mailHost/n";}??????????? #The smtp hostmy @mailTo = @$mailToRef;?? #The recipient listfor(my $i=0; $i<=$#mailTo; $i++) {???? print "$#mailTo/n";???? print "$mailTo[$i]/n";?? }my @attachment = @$attachmentRef;??? #The attachmentsmy $smtp = Net::SMTP->new($mailHost,Hello => $helloPara,Timeout => 120,Debug => 1)??????? ||die 'Cannot connect to server /'$mailHost/'';# anth login,type your user name and password here? $smtp->auth($userName,$password)||print "Auth Error!/n";? foreach my $mailTo (@mailTo)? {??? # Create a new multipart message:??? my $msg = MIME::Lite->new(??????? From??? => $mailFrom,??????? To????? => $mailTo,??????? Subject => $subject,??????? Type??? =>'multipart/mixed',??????? )or print "Error creating MIME body: $!/n";??? # Add parts:??? $msg->attach(Type???? =>'TEXT',???????????????? Data??? => $content,??????????????? );??? foreach my $attachment (@attachment)??? {??????? $msg->attach(????????? Type???? => 'AUTO',????? # the attachment mime type????????? Path???? => $attachment,# local address of the attachment????????? )or print "Error attaching test file: $!/n";??? }??? my $str = $msg->as_string() or print "Convert the message as a string: $!/n";??????? # Send the From and Recipient for the mail servers that require it??????? $smtp->mail($mailFrom);??????? $smtp->to($mailTo);??????? # Start the mail??????? $smtp->data();??????? # Send the message??????? $smtp->datasend("$str");??????? # Send the termination string??????? $smtp->dataend();}$smtp->quit;return;}my $drvTestReport="/data/test";our $mailFrom = 'dh03@163.com';?????? #Send email addressour $password = 'XXX';my @mailTo = ('duhui@goso.cn');? #The recipient listmy $mailSubject = "Driver Test Result";my $mailContent = "The attachment is the $mailSubject!/nPlease check it./nThanks!/n/n????? Chenny.chen/n";my @mailAttachment = ($drvTestReport);??? #The attachments for the test resultSendMail($mailFrom,@mailTo,$mailSubject,$mailContent,@mailAttachment); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |