0

I am trying to send email using Net::SMTPS and 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.";

The output when running this script (Ubuntu 16.04, Perl version 5.22.1) is:

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

I wonder why I get this error:

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.

when trying to authenticate?

Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174
  • 1
    Have you tried to use Net::SMTP? AFAIR versions of Net::SMTP above 3.0 support SMTPS and STARTLS. – AnFi Sep 26 '16 at 16:34
  • @AndrzejA.Filip Thanks for the information. I tried `Net::SMTP` now and it also fails at `$smtp->auth( $user, $password )`. This time there is no internal syntax error as in `Net::SMTPS`, instead it returns `undef` indicating a failure to authenticate. I noticed that there is also a `$smtp->auth ( SASL )` where you can supply a `Authen::SASL` object instead. But I am not sure how to do that based on the documentation in [`Authen::SASL::XS`](https://metacpan.org/pod/Authen::SASL::XS) – Håkon Hægland Sep 26 '16 at 17:26

1 Answers1

1

Try the code below. Change user and password.
It connects to SMTPS. It seems to work on my linux.
Net::SMTP requires $smtp->starttls() to execute STARTTLS command.

#!/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();
AnFi
  • 10,493
  • 3
  • 23
  • 47
  • Thanks for the code, but it does not work. Still the authentication is the problem. Here is the output I get: http://pastebin.com/JEMCKHNy – Håkon Hægland Sep 26 '16 at 19:28
  • My last guess: Try to force use of Authen::SASL::Perl -> `use Authen::SASL qw(Perl);`. Authen::SASL::Perl is implemented in perl and it supports `PLAIN` and `LOGIN` methods. – AnFi Sep 27 '16 at 04:30
  • Thanks again for your time. Using `Authen::SASL::Perl` it still does not work, but I get more information when trying to authenticate now. [Here](http://pastebin.com/MfUjiwkA) is the script, and [here](http://pastebin.com/WcCWz1Fc) is the output. Now I get the message: `Please log in via your web browser and then try again.`. I think I maybe need to use XOAuth2 login, but it seems that it is not supported by `Authen::SASL`. I am able to use [python-oauth2](https://github.com/joestump/python-oauth2) to send mail with `smtp.gmail.com` at port 587, but I wonder how to do this in Perl? – Håkon Hægland Sep 27 '16 at 10:06
  • It MAY be OK thus time. It seems that google locked out your email account for some reason - log via web browser as suggested. CHANGE YOUR PASSWORD. – AnFi Sep 27 '16 at 13:25
  • Hmm.. No, actually it seems it is not related to a lockout from google. I tried to change my password now, and I get exactly the same message when trying to authenticate. Also, if I try give a wrong password, I get `Username and Password not accepted.` from `$smtp->auth()` so it clearly accepts my username and password, but later decides that the SASL `LOGIN` mechanism is not good enough anyway.. (Also I am able to send email using a similar python script as I said in the comment above, except that I used XOAUTH2) – Håkon Hægland Sep 27 '16 at 14:04
  • **Temporarily** turn off google two step verification - check if it makes difference https://support.google.com/accounts/answer/1064203?hl=en , If it makes difference then **carefully** consider turning it off permanently. – AnFi Sep 27 '16 at 14:35
  • No that is not the issue either. I already have it turned that off. I rather suspect that it is the sign-in security policy, see [this](http://stackoverflow.com/questions/10013736/how-can-i-avoid-google-mail-server-asking-me-to-log-in-via-browser/33244509#33244509) question. I have not allowed less secure apps in my [account setup](https://myaccount.google.com/security?pli=1). I will check if this is the case later today, and come back with more information. – Håkon Hægland Sep 27 '16 at 16:03
  • Yes, I can confirm that it works if I allow less secure apps in my account setup. But this is not what I want. I do not want to reduce the security level for my account just in order to be able to send emails. I know it is possible to send email using XOAUTH2 in Python, so I would like to find out if it is possilble to do it in Perl also. Thanks for all the help an have a nice evening! – Håkon Hægland Sep 27 '16 at 18:52