#cat send_mail.pl
#!/usr/bin/perl -w
#Author:duhui
#Date :2010-11-29
#perl -MCPAN -e shell
#cpan>install Net::SMTP_auth
use Net::SMTP;
use Net::SMTP_auth;
use MIME::Base64;
$mail_server = 'smtp.163.com';
$mail_from = '[email protected]';
$mail_to = '[email protected]';
$uname='XXX';
$passwd='XXX';
#开启Debug模式
$smtp = Net::SMTP->new("$mail_server",Debug => 1);
#普通发送模式
#$smtp = Net::SMTP->new("$mail_server" );
$smtp->auth("$uname","$passwd");
$smtp->mail("$mail_from");
$smtp->to("$mail_to");
$smtp->data();
$smtp->datasend("from:$mail_from");
$smtp->datasend("To:$mail_to");
$smtp->datasend("Reply-To:$mail_from");
$smtp->datasend("Return-Path:$mail_from");
$smtp->datasend('Subject:Your Subject');
$smtp->datasend('Content-Type:text/plain; ChartSet=gb2312');
$smtp->datasend('Your mail content!');
$smtp->datasend('/n');
$smtp->quit;
另外一种实现方式 可以发送附件的
#*****************************************************************************## 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 = '[email protected]'; #Send email addressour $password = 'XXX';my @mailTo = ('[email protected]'); #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);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。