perl使用Net::SMTP_auth模块发送邮件
cpan下有不少邮件发送模块,但说实话,好用且简单的少,经过一番对比和试用,发现Net::SMTP_auth模块正是我想要的。Net::SMTP_auth - Simple Mail Transfer Protocol Client with AUTHentication
This module implements a client interface to the SMTP and ESMTP protocol AUTH service extension, enabling a perl5 application to talk to and authenticate against SMTP servers. This documentation assumes that you are familiar with the concepts of the SMTP protocol described in RFC821 and with the AUTH service extension described in RFC2554.
该模块实现了到smtp和esmtp协议的认证扩展,是在perl5下可与smtp服务器认证的客户实现。遵守rfc821及rfc2554。
The Net::SMTP_auth class is a subclass of Net::SMTP, which itself is a subclass of Net::Cmd and IO::Socket::INET.
Net::SMTP_auth类是Net::SMTP的子类,同时也需要Net::Cmd 与 IO::Socket::INET这两类模块。
可用的方法
auth_types ()
Returns the AUTH methods supported by the server as an array or in a space separated string. This string is exacly the line given by the SMTP server after the EHLO command containing the keyword AUTH.
返回远程的smtp服务器可支持的认证方法,为空格分开的数组类型,即在发出'ehlo'指令后的返回字串。
auth ( AUTH, USER, PASSWORD )
Authenticates the user USER via the authentication method AUTH and the password PASSWORD. Returns true if successful and false if the authentication failed. Remember that the connection is not closed if the authentication fails. You may issue a different authentication attempt. If you once are successfully authenticated, you cannot send the AUTH command again.
向远程smtp服务器发送认证请求,其中包含了用户名及密码,成功则继续否则需要重新认证,但这个过程中并不会关闭连接。
The AUTH method NTLM is supported via Authen::NTLM (thanks to James Fryman).
示例
use Net::SMTP_auth;
$smtp = Net::SMTP_auth->new('freeoa.net',Hello => 'freeoa.net',Debug => 1);
$smtp->auth('LOGIN', 'loveus@freeoa.net', 'loveus_pass');
#mail from
$smtp->mail('loveus@freeoa.net');
#mail to
$smtp->to('loveus@freeoa.net');
$smtp->data();
$smtp->datasend("Subject:测试邮件\n");
#mail_to
$smtp->datasend("loveus\@freeoa.net");
#注意这里要两个换行,不然不会显示邮件正文内容。
$smtp->datasend("\n");
$smtp->datasend("\n");
$smtp->datasend("来自测试邮件的正文 \n");
$smtp->dataend();
$smtp->quit;
网上的有问题因为datasend的顺序不正确,subject应该在前面,还有datasend里面不用再加from,下面封装一下发送函数。
sub mail_sender{
my ($subject,$mesg)=(@_);
my $smtp=Net::SMTP_auth->new('mail.freeoa.net',Hello => 'mail.freeoa.net',Debug=>1);
$smtp->auth('LOGIN','monitor@freeoa.net','123.com');
#mail from
$smtp->mail('monitor@freeoa.net');
#mail to
$smtp->to('sa-team@freeoa.net');
#$smtp->to('zhengjiong@xd-tech.cn');
$smtp->send_and_mail('monitor@freeoa.net');
#####
$smtp->data();
$smtp->datasend("From: monitor\@freeoa.net\n");
$smtp->datasend("To: sa-team\@freeoa.net\n");
$smtp->datasend("Subject:$subject \n");
#mail_to
#$smtp->datasend("monitor\@freeoa.net");
$smtp->datasend("\n");
$smtp->datasend("\n");
$smtp->datasend("$mesg \n");
$smtp->dataend();
$smtp->quit;
}
## mail send fun end
perldoc Net::SMTP_auth 里面显示的实例是:
#!/usr/bin/perl -w
use Net::SMTP_auth;
$smtp = Net::SMTP_auth->new('mailhost');
$smtp->auth('CRAM-MD5′, 'user', 'password');
$smtp->mail($ENV{USER});
$smtp->to('postmaster');
$smtp->data();
$smtp->datasend("To: postmaster\n");
$smtp->datasend("\n");
$smtp->datasend("A simple test message\n");
$smtp->dataend();
$smtp->quit;
也差不多,这样我们就可以自己通过perl来发送邮件了。
参考链接
http://search.cpan.org/~apleiner/Net-SMTP_auth-0.08/SMTP_auth.pm
http://lvdbing.blog.163.com/blog/static/55801529200832111630600/
http://blog.csdn.net/dongdong163/article/details/7406504
http://www.litvip.com/569/
http://luckylarry.co.uk/programming-tutorials/perl/sending-mail-and-attachments-from-perl-using-netsmtp/