天天看點

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