Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

gmail sending in 2020

by iaw4 (Monk)
on Jan 28, 2020 at 20:40 UTC ( [id://11111983]=perlquestion: print w/replies, xml ) Need Help??

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

This has been asked and answered many times, but Google keeps changing its interface and cpan continues carrying many modules that no longer work. The combination of the two, plus non-descript failure messages, make getting this to work quite painful.

As of 2020, can someone please post a short unix cli that actually works and successfully sends email from a gmail account, knowing the google account and password? I am thinking

$ perl sendgmail.pl sender@gmail.com senderpassword \ recipient@anywhere.com subject "body text"
(assumes no 2FA for google. if it requires extra steps on google itself, please post with it.)

Replies are listed 'Best First'.
Re: gmail sending in 2020
by 1nickt (Canon) on Jan 29, 2020 at 02:20 UTC

    Hi,

    Last time this was discussed (including Google setup step).

    Hope this helps!


    The way forward always starts with a minimal test.
Re: gmail sending in 2020
by clueless newbie (Curate) on Jan 29, 2020 at 19:18 UTC

    I tried the code in the post noted above without success (session error: 5.7.8 Username and Password not accepted. Learn more at...) (Windows 10) until ...

    "As it turns out, there are TWO "Allow Less Secure Apps" toggles which need to be changed to allow logins from unknown devices/IMAP.

    One here: https://myaccount.google.com/security?pli=1#connectedapps (bottom of the page)

    And one here: https://www.google.com/settings/security/lesssecureapps

    BOTH OF THESE GODFORSAKEN TOGGLES need to be changed to get rid of this error message."

    See this stackoverflow post.
      thanks, both of you. I will try it now. last time it was discussed was 4 years ago. sometimes, google changes stuff every other week, sometimes never. let's see if I can make it work. if so, I will post something here that summarizes it.
        As of Jan 29, 2020, the following works.
        1. Turn off 2FA .
        2. on 'https://myaccount.google.com/security?pli=1#connectedapps', look for and allow Less secure app access
        3. on 'https://myaccount.google.com/lesssecureapps', same thing.
        4. use the slightly elaborated perl program:
        #!/usr/bin/env perl use strict; use warnings; use autodie; use Data::Dumper; my $gauthtxt= "gauth.txt"; # can define default sending user account +and password ## format: 'user\temail\npasswd\tstring\n' ################################################################ # parse inputs # defaults my %smtp= ( server => 'smtp.gmail.com', port => '587', subject => "tes +ting gmailer.pl", body => "gmail-tester $0: ".localtime() ); # more defaults in gauth.txt if (-e $gauthtxt) { print STDERR "[reading $gauthtxt]\n"; open(my $GA, "<", $gauthtxt); while (<$GA>) { chomp; (/^([a-z]+)\s+(.*)$/) or die "line in $gauthtxt is not pars +eable: $_\n"; $smtp{$1}= $2; } close($GA); } # command line arguments foreach (@ARGV) { foreach my $keyword (qw(user to passwd subject body)) { (/^$keyword= +(.*)/) and $smtp{$keyword}=$1; } } (exists{$smtp{from})) and $smtp{user}=$smtp{from}; ## special equivale +nce ################################################################ # check inputs foreach (qw(user to passwd subject body)) { exists($smtp{$_}) or die "'$_' needs to be in $gauthtxt or spcified +on command line with '$_=...'\n"; } use Email::Valid; (Email::Valid->address($smtp{user})) or die "your own user account $sm +tp{user} is not a valid email address"; (Email::Valid->address($smtp{to})) or die "your recipientd account $sm +tp{to} is not a valid email address"; ################################################################ # send email use Email::Sender::Simple qw(sendmail); use Email::Sender::Transport::SMTPS (); use Email::Simple (); use Email::Simple::Creator (); my $transport = Email::Sender::Transport::SMTPS->new({ host => $smtp{server}, port => $smtp{port}, ssl => "starttls", sasl_username => $smtp{user}, sasl_password => $smtp{passwd}, }); my $email = Email::Simple->create( header => [ To => $smtp{to}, From => $smtp{user}, Subject => $smtp{subject} ], body => $smtp{body}."\n", ); sendmail($email, { transport => $transport }); print "\nemail sent from $smtp{user} to $smtp{to}.\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11111983]
Approved by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 23:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found