2016-09-26 29 views
0

我试图发送使用Net::SMTPS和Gmail电子邮件:

#! /usr/bin/env perl 

use feature qw(say); 
use strict; 
use warnings; 

use Net::SMTPS; 

my $smtp = Net::SMTPS->new(
    'smtp.gmail.com', 
    Port => 587, 
    Timeout => 20, 
    Debug => 1, 
    doSSL => 'starttls', 
); 
die "Initialization failed: $!" if !defined $smtp; 

my $sender = my $user = '[email protected]'; 
my $password = '????';  
say "Trying to authenticate.."; 
$smtp->auth($user, $password, 'LOGIN' ) or die "could not authenticate\n"; 

my $receiver = '[email protected]';  
$smtp->mail($sender); 
$smtp->to($receiver); 
$smtp->data(); 
$smtp->datasend("To: $receiver\n"); 
$smtp->datasend("From: $sender\n"); 
$smtp->datasend("Content-Type: text/html\n"); 
$smtp->datasend("Subject: Testing Net::SMTPS"); 
$smtp->datasend("\n"); 
$smtp->datasend('The body of the email'); 
$smtp->dataend(); 
$smtp->quit(); 
say "Done."; 

输出运行此脚本(Ubuntu的16.04,Perl版本5.22.1时)是:

Net::SMTPS>>> Net::SMTPS(0.04) 
Net::SMTPS>>> IO::Socket::INET6(2.72) 
Net::SMTPS>>>  IO::Socket(1.38) 
Net::SMTPS>>>  IO::Handle(1.35) 
Net::SMTPS>>>   Exporter(5.72) 
Net::SMTPS>>> Net::SMTP(3.05) 
Net::SMTPS>>>  Net::Cmd(3.05) 
Net::SMTPS>>>  IO::Socket::IP(0.37) 
Net::SMTPS=GLOB(0x10b2dd8)<<< 220 smtp.gmail.com ESMTP x131sm3965376lff.44 - gsmtp 
Net::SMTPS=GLOB(0x10b2dd8)>>> EHLO localhost.localdomain 
Net::SMTPS=GLOB(0x10b2dd8)<<< 250-smtp.gmail.com at your service, [195.139.193.120] 
Net::SMTPS=GLOB(0x10b2dd8)<<< 250-SIZE 35882577 
Net::SMTPS=GLOB(0x10b2dd8)<<< 250-8BITMIME 
Net::SMTPS=GLOB(0x10b2dd8)<<< 250-STARTTLS 
Net::SMTPS=GLOB(0x10b2dd8)<<< 250-ENHANCEDSTATUSCODES 
Net::SMTPS=GLOB(0x10b2dd8)<<< 250-PIPELINING 
Net::SMTPS=GLOB(0x10b2dd8)<<< 250-CHUNKING 
Net::SMTPS=GLOB(0x10b2dd8)<<< 250 SMTPUTF8 
Net::SMTPS=GLOB(0x10b2dd8)>>> STARTTLS 
Net::SMTPS=GLOB(0x10b2dd8)<<< 220 2.0.0 Ready to start TLS 
Net::SMTPS=GLOB(0x10b2dd8)>>> EHLO localhost.localdomain 
Net::SMTPS=GLOB(0x10b2dd8)<<< 250-smtp.gmail.com at your service, [195.139.193.120] 
Net::SMTPS=GLOB(0x10b2dd8)<<< 250-SIZE 35882577 
Net::SMTPS=GLOB(0x10b2dd8)<<< 250-8BITMIME 
Net::SMTPS=GLOB(0x10b2dd8)<<< 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH 
Net::SMTPS=GLOB(0x10b2dd8)<<< 250-ENHANCEDSTATUSCODES 
Net::SMTPS=GLOB(0x10b2dd8)<<< 250-PIPELINING 
Net::SMTPS=GLOB(0x10b2dd8)<<< 250-CHUNKING 
Net::SMTPS=GLOB(0x10b2dd8)<<< 250 SMTPUTF8 
Trying to authenticate.. 
Use of uninitialized value $_ in pattern match (m//) at /usr/share/perl/5.22/Net/Cmd.pm line 250. 
Use of uninitialized value in join or string at /usr/share/perl/5.22/Net/Cmd.pm line 249. 
Net::SMTPS=GLOB(0x10b2dd8)>>> AUTH 
Net::SMTPS=GLOB(0x10b2dd8)<<< 555 5.5.2 Syntax error. x131sm3965376lff.44 - gsmtp 
could not authenticate 

我不知道为什么我得到这个错误:

Use of uninitialized value $_ in pattern match (m//) at /usr/share/perl/5.22/Net/Cmd.pm line 250. 
Use of uninitialized value in join or string at /usr/share/perl/5.22/Net/Cmd.pm line 249. 

尝试进行身份验证时?

+1

您是否尝试过使用Net :: SMTP? AFAIR版本的Net :: SMTP 3.0以上版本支持SMTPS和STARTLS。 – AnFi

+0

@ AndrzejA.Filip谢谢你的信息。我现在尝试了'Net :: SMTP',并且在'$ smtp-> auth($ user,$ password)'时也失败了。这次在'Net :: SMTPS'中没有内部语法错误,而是返回'undef',表示验证失败。我注意到还有一个'$ smtp-> auth(SASL)',您可以在其中提供一个'Authen :: SASL'对象。但我不知道如何做到这一点基于['Authen :: SASL :: XS'](https://metacpan.org/pod/Authen::SASL::XS)中的文档 –

回答

1

请尝试下面的代码。更改用户和密码。
它连接到SMTPS。它似乎在我的linux上工作。
Net :: SMTP需要$ smtp-> starttls()来执行STARTTLS命令。

#!/usr/local/bin/perl 
use strict; 
use warnings; 
use utf8; 

use Net::SMTP 3.0; 
# Force use of Authen::SASL::Perl - it may fix problems with other alternatives 
use Authen::SASL qw(Perl); 

my $smtp = Net::SMTP->new(
    'smtp.gmail.com', 
    SSL=>1, 
    Timeout => 20, 
    Debug => 1, 
); 
die "Initialization failed: $!" if !defined $smtp; 

my $sender = my $user = '[email protected]'; 
my $password = '????'; 
print "Trying to authenticate.."; 
$smtp->auth($user, $password) or die "could not authenticate\n"; 

my $receiver = '[email protected]'; 
$smtp->mail($sender); 
$smtp->to($receiver); 
$smtp->data(); 
$smtp->datasend("To: $receiver\n"); 
$smtp->datasend("From: $sender\n"); 
$smtp->datasend("Content-Type: text/html\n"); 
$smtp->datasend("Subject: Testing Net::SMTP"); 
$smtp->datasend("\n"); 
$smtp->datasend('The body of the email'); 
$smtp->dataend(); 
$smtp->quit(); 
+0

感谢您的代码,但它不起作用。仍然认证是问题。下面是我得到的输出:http://pastebin.com/JEMCKHNy –

+0

我最后的猜测:尝试强制使用Authen :: SASL :: Perl - >'使用Authen :: SASL qw(Perl);'。 Authen :: SASL :: Perl是在perl中实现的,它支持'PLAIN'和'LOGIN'方法。 – AnFi

+0

再次感谢您的时间。使用'Authen :: SASL :: Perl'它仍然不起作用,但我现在尝试进行身份验证时会获得更多信息。 [Here](http://pastebin.com/MfUjiwkA)是脚本,[here](http://pastebin.com/WcCWz1Fc)是输出。现在我收到消息:'请通过您的网络浏览器登录,然后再试一次。“我想我可能需要使用XOAuth2登录,但似乎它不被'Authen :: SASL'支持。我可以使用[python-oauth2](https://github.com/joestump/python-oauth2)在端口587使用'smtp.gmail.com'发送邮件,但我想知道如何在Perl中执行此操作? –