manojsagar has asked for the wisdom of the Perl Monks concerning the following question:

Trying to send an email using my personal gmail using perl various modules (MIME::Lite, Email::Send::SMTP::Gmail, Email::Simple::Creator,Email::Sender::Transport::SMTPS), but keep getting the response 'Network Unreachable'.

Here is sample code:

!/usr/bin/perl use strict; use warnings; use Email::Sender::Simple qw(sendmail); use Email::Sender::Transport::SMTPS; use Email::Simple (); use Email::Simple::Creator (); my $smtpserver = 'smtp.gmail.com'; my $smtpport = 587; my $smtpuser = 'myuser@gmail.com'; my $smtppassword = 'myuser@gmail.com password'; my $transport = Email::Sender::Transport::SMTPS->new({ host => $smtpserver, ssl => 'starttls', port => $smtpport, sasl_username => $smtpuser, sasl_password => $smtppassword, debug => 1, }); my $email = Email::Simple->create( header => [ To => 'xxxxx@gmail.com', From => 'myuser@gmail.com', Subject => 'Hi!', ], body => "This is my message\n", ); sendmail($email, { transport => $transport });

Response:

unable to establish SMTP connection

Any help would be appreciated..

Replies are listed 'Best First'.
Re: Network Unreachable issue while sending email using PERL (gmail)
by 1nickt (Canon) on Oct 12, 2017 at 17:59 UTC

    Hello, first you will need to go into the security setting of your Gmail account and create an "application" password for your script. (This assumes your account is currently set to two-factor auth.) See for example this doc.

    Then use Email::Send::SMTP::Gmail:

    use strict; use warnings; use Email::Send::SMTP::Gmail; my ( $email, $error ) = Email::Send::SMTP::Gmail->new( -port => 587, -smtp => 'smtp.gmail.com', -login => 'you@gmail.com', -pass => 'passwordyoucreated' ); die "SMTP connection error: $error" if $email == -1; $email->send( -to => 'you@gmail.com', -subject => 'This is the subject', -body => 'This is the message body', ); $email->bye; __END__

    Hope this helps!


    The way forward always starts with a minimal test.

      I managed to resolve it using Email::Sender::Transport::SMTP

      #!/usr/bin/perl use strict; use warnings; use Email::Sender::Transport::SMTP; use Email::Sender::Simple qw(sendmail); my $smtpserver = 'smtp.gmail.com'; my $smtpport = 587; my $smtpuser = 'xxxxxxxx@gmail.com'; my $smtppassword = 'xxxxxxxxx'; my $transport = Email::Sender::Transport::SMTP->new({ host => $smtpserver, ssl => 'starttls', port => $smtpport, sasl_username => $smtpuser, sasl_password => $smtppassword, debug => 1, }); my $email = Email::Simple->create( header => [ To => 'xxxxxxxxxx', From => 'xxxxxx@gmail.com', Subject => 'Hi!', ], body => "This is my message\n", ); sendmail($email, { transport => $transport });

        It's also a nice solution, one I have used in the past because you can wrap the Transport agent code in its own module so the credentials and whatnot are not exposed in the application code.

        use Email::Sender::Simple qw(sendmail); use My::Email::TransportAgent; # returns instance of Email::Sender::Tr +ansport::SMTP ... sendmail( $email, { transport => My::Email::TransportAgent->new } );

        (Or create My::Email::Sender and wrap the whole thing ...)


        The way forward always starts with a minimal test.
Re: Network Unreachable issue while sending email using PERL (gmail)
by sweetblood (Prior) on Oct 12, 2017 at 16:27 UTC