Perl使用Net::SMTP_auth认证发送邮件
#!/usr/bin/perl -w # http://perldoc.perl.org/Net/SMTP.html # http://search.cpan.org/~apleiner/Net-SMTP_auth-0.08/SMTP_auth.pm use strict; use threads; use Net::SMTP_auth; my $emlPath="@EML_PATH_VAR@"; my $smtpSvrIP = "@SMTP_SVR_IP_VAR@"; my $LocalBindIP = "@LOCAL_BIND_IP_VAR@"; my $heloDomain="@HELO_DOMAIN_VAR@"; my $authUsername="@AUTH_USERNAME_VAR@"; my $authPassword="@AUTH_PASSWORD_VAR@"; my $mailFrom = "@MAIL_FROM_VAR@"; my $rcptTo1 = "@RCPT_TO_VAR@"; my $needRetry = 0; sub getDateTime { my $tTime = shift || time(); my ($second, $minute, $hour, $monthday, $month, $year, $weekday, $yearday, $isdst) = localtime($tTime); $month++; $second = ($second < 10) ? "0$second" : $second; $minute = ($minute < 10) ? "0$minute" : $minute; $hour = ($hour < 10) ? "0$hour" : $hour; $monthday = ($monthday < 10) ? "0$monthday" : $monthday; $month = ($month < 10) ? "0$month" : $month; $year += 1900; return "$year-$month-$monthday $hour:$minute:$second"; } sub printResult { #my ($args) = @_; my ($beginTime, $strCmd, $smtpConnector) = @_; #my $smtpConnector = shift; my $nowTime = &getDateTime(); my $spendTime = time - $beginTime; my $retCode = $smtpConnector->code(); my $retMessage = $smtpConnector->message(); if ( $needRetry == 0 && $retCode > 400 && $retCode < 500 ) { $needRetry = 1; print "[$nowTime] cmd:$strCmd,from:$mailFrom,to:$rcptTo1,server:$smtpSvrIP,path:$emlPath,respond:$retCode $retMessage,spend:$spendTime, will retry\n"; return; } if ( $strCmd eq "DATA" ) { my $newLinePos = index($retMessage, "\n"); $retMessage = substr($retMessage, $newLinePos + 1); } chop $retMessage; print "[$nowTime] cmd:$strCmd,from:$mailFrom,to:$rcptTo1,server:$smtpSvrIP,path:$emlPath,respond:$retCode $retMessage,spend:$spendTime\n"; } sub smtpSendSingleEML { open(EMLFILE, $emlPath) || die "Cannot open eml file: $emlPath($!)"; my $beginTime = time; my $smtpConnector = Net::SMTP_auth->new($smtpSvrIP, Port => 25, Hello => $heloDomain, Timeout => 60, LocalAddr => $LocalBindIP, Debug => 0); if ( defined($smtpConnector) ) { if ( $authUsername ne "" ) { if ( ! ($smtpConnector->auth('LOGIN', $authUsername, $authPassword)) ) { printResult($beginTime, "AUTH", $smtpConnector); return 0; } } if ( ! ($smtpConnector->mail($mailFrom)) ) { printResult($beginTime, "MAIL", $smtpConnector); return 0; } if ( ! ($smtpConnector->to($rcptTo1)) ) { printResult($beginTime, "RCPT", $smtpConnector); return 0; } if ( ! ($smtpConnector->data()) ) { printResult($beginTime, "DATABEGIN", $smtpConnector); return 0; } while( <EMLFILE> ) { chop $_; $smtpConnector->datasend("$_\n"); } $smtpConnector->dataend(); printResult($beginTime, "DATA", $smtpConnector); $smtpConnector->quit(); } else { my $nowTime = &getDateTime(); my $spendTime = time - $beginTime; print "[$nowTime] cmd:HELO,from:$mailFrom,to:$rcptTo1,server:$smtpSvrIP,path:$emlPath,respond:550 Connect to server fail,spend:$spendTime\n"; } close(EMLFILE); } =comment sub smtpSendByFolder { my $sendEmlCnt = 0; chdir($emlDir) || die "Cannot chdir to $emlDir: ($!)"; for( my $i = 1; $i <= $cycleCnt; $i++ ) { while( glob("*.eml") ) { $sendEmlCnt++; $emlPath = "$emlDir/$_"; smtpSendSingleEML(); } } } $SIG{'PIPE'} = 'IGNORE'; my @threadpool; for( my $i = 1; $i <= $threadCnt; $i++ ) { my $thread = threads->create(\&smtpSendByFolder, "Thread: $i"); push(@threadpool, \$thread); } foreach my $thread (@threadpool) { $$thread->join(); } =cut smtpSendSingleEML(); if ( $needRetry == 1 ) { sleep 60; smtpSendSingleEML(); }
几点说明:
1,发送邮件前,需要替换文件头的 "@SMTP_SVR_IP_VAR@" 等变量
2,可自动识别是否需要认证,即可连接smtp服务器或mx服务器发信
3,安装perl后默认安装Net::SMTP模块,但没有安装Net::SMTP_auth模块,需要另外安装
4,Net::SMTP_auth要用到Digest::HMAC_MD5和Authen::SASL,需要另外安装
5,所有perl模块都可以在http://search.cpan.org找到下载,下载后解开安装包,执行perl Makefile.pl && make && make install可完成安装,或将*.pm拷贝到/usr/lib/perl5/5.8.8相应的目录下完成安装
6,查看系统已经安装的perl模块的方法:find /usr/lib/perl5/5.8.8 | grep .pm
相关推荐
边城客栈学无止境 2020-07-05
Walter的学习笔记 2020-07-04
A宇 2020-06-14
边城客栈学无止境 2020-06-10
邓博学习笔记 2020-06-03
davidliu00 2020-05-26
ShiShuo 2020-05-16
Aggressivesnail 2020-05-10
ShiShuo 2020-04-26
hanxingwang00 2020-04-22
davidliu00 2020-03-06
ShiShuo 2020-03-06
ShiShuo 2020-03-05
Aggressivesnail 2020-02-28
aaLiweipeng 2020-02-01
amberom 2020-01-16
Walter的学习笔记 2020-01-06