使用Gmail SMTP服务器的(Perl Net :: SMTP)身份验证失败

使用Gmail SMTP服务器的(Perl Net :: SMTP)身份验证失败

问题描述:

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

I have written a perl script to send email using the gmail's smtp server: 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";

对于用户",我有我的gmail用户名,对于"mypassword",我有密码.但是,当我运行此代码时,它停止在auth本身给出:无法进行身份验证.

For 'user', I have my gmail username and for 'mypassword' I have the password. But when I run this code it stops at the auth itself giving : could not authenticate.

当我得到:'mx.google.com'的结果时,我能够连接到google的smtp服务器:
打印$ smtp->域,"\ n";

I am able to connect to the smtp serever of google as I get : 'mx.google.com' as the result of :
print $smtp->domain,"\n";

我做错了什么?请帮忙.

What is it that I am doing wrong? Please help.

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

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

afaik you need to use an encrypted connection to send via gmail, use Net::SMTP::TLS or Net::SMTP::SSL (with port 465)

Hello=>'user@gmail.com'

"Hello"不是电子邮件地址,请在其中输入您的主机名

'Hello' is not an email address, put your hostname there instead

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

将这些放在单引号中

如果您仍然收到无法验证".确保已安装模块 MIME :: Base64 Authen :: SASL .

if you still get "could not authenticate." make sure you have the modules MIME::Base64 and Authen::SASL installed.

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

应该是 $ receiver ,而不是 $ reciever