当前位置:首页 > 开发 > 软件应用 > 正文

Perl使用Net::SMTP_auth认证发送邮件

发表于: 2012-08-29   作者:cloudmail   来源:转载   浏览:
摘要:   #!/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

 

#!/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


 

Perl使用Net::SMTP_auth认证发送邮件

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号