(perl Net :: SMTP)使用gmail smtp服务器验证失败

时间:2012-06-13 07:10:31

标签: perl smtp gmail

我编写了一个perl脚本,使用gmail的smtp服务器发送电子邮件:smtp.gmail.com -

use Net::SMTP;

my $smtp = Net::SMTP->new('smtp.gmail.com',
                    Port=> 587,
                    Timeout => 20,
                    Hello=>'user@gmail.com'
                    );
print $smtp->domain,"\n";
$sender = "user@gmail.com";
$password = "mypassword";
$smtp->auth ( $sender, $password ) or die "could not authenticate\n";
$receiver = "user@gmail.com";

$subject = "my custom subject";
$smtp->mail($sender);
$smtp->to($receiver);
$smtp->data();
$smtp->datasend("To: <$reciever> \n");
$smtp->datasend("From: <$sender> \n");
$smtp->datasend("Content-Type: text/html \n");
$smtp->datasend("Subject: $subject");
$smtp->datasend("\n");
$smtp->datasend('the body of the email');
$smtp->dataend();
$smtp->quit();
print "done\n\n";

对于'user',我有我的gmail用户名,对于'mypassword',我有密码。但是,当我运行此代码时,它会停止在auth本身,因为它无法进行身份验证。

我能够连接到google的smtp serever:'mx.google.com',因为: 打印$ smtp-&gt;域名,“\ n”;

我做错了什么?请帮忙。

2 个答案:

答案 0 :(得分:3)

use Net::SMTP;
my $smtp = Net::SMTP->new ....

afaik你需要使用加密连接通过gmail发送,使用 Net :: SMTP :: TLS Net :: SMTP :: SSL (使用端口465 )

Hello=>'user@gmail.com'

'Hello'不是电子邮件地址,而是将主机名放在那里

$sender = "user@gmail.com";
...
$receiver = "user@gmail.com";

将它们放在单引号中

如果您仍然“无法进行身份验证”。确保安装了模块 MIME :: Base64 Authen :: SASL

 $smtp->datasend("To: <$reciever> \n");

应该是 $ receiver ,而不是 $ reciever

答案 1 :(得分:0)

Gmail使用TLS/STARTTLS on port 587, SSL on port 465。请关注Sending email through Google SMTP from Perl。或者,您可以使用Email::Send::SMTP::Gmail。我建议这样做,因为它更容易使用,并具有更多功能。

相关问题