加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl 发送邮件 可以发送附件

发布时间:2020-12-15 20:51:48 所属栏目:大数据 来源:网络整理
导读:#cat send_mail.pl #!/usr/bin/perl -w #Author:duhui #Date :2010-11-29 #perl -MCPAN -e shell #cpaninstall Net::SMTP_auth use Net::SMTP; use Net::SMTP_auth; use MIME::Base64; $mail_server = 'smtp.163.com'; $mail_from = 'dh03@163.com'; $mail_t

#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 = 'dh03@163.com';
$mail_to = 'duhui@goso.cn';
$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 = '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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读