使用Net :: SMTPS发送电子邮件:使用未初始化的值$ _

时间:2016-09-26 15:02:17

标签: perl sasl

我正在尝试使用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 = 'hakon.hagland@gmail.com';
my $password = '????';    
say "Trying to authenticate..";
$smtp->auth( $user, $password, 'LOGIN'  ) or die "could not authenticate\n";

my $receiver = 'hakon.hagland@gmail.com';    
$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 个答案:

答案 0 :(得分:1)

尝试以下代码。更改用户和密码 它连接到SMTPS。它似乎适用于我的linux Net :: SMTP需要$ smtp-&gt; 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 = 'john.doe@gmail.com';
my $password = '????';
print "Trying to authenticate..";
$smtp->auth( $user, $password) or die "could not authenticate\n";

my $receiver = 'john.doe@gmail.com';
$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();