Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Send email via Gmail

by stevieb (Canon)
on Oct 08, 2016 at 14:27 UTC ( [id://1173536]=note: print w/replies, xml ) Need Help??


in reply to Send email via Gmail

I've had the need to do this in the past, but gave up as I didn't have the time to troubleshoot. I did this morning :) Here's how I got it to work:

# install some seemingly required modules cpan MIME::Base64 cpan Authen::SASL

In your gmail account, you have to allow the use of less-secure apps, as per this. I was led to that after putting a or die $!; after the auth call. Of course, all email addresses in the example script below have been changed from my real personal one.

UPDATE: Less secure apps is no longer allowed. You now have to set up an App Password. To do so, your Google account needs to have 2FA enabled. Setting up an app password is very simple, so I'll allow you to find the documentation yourself. The asterisks in the auth() call before used to be my password, but this is where you simply replace it with the App key you generate in the Security section of your Google account. Nothing else changes.

use warnings; use strict; use Net::SMTP; my $smtp = Net::SMTP->new( 'smtp.gmail.com', Hello => 'local.example.com', # can be anything Timeout => 30, Debug => 1, SSL => 1, ); $smtp->auth('me@gmail.com', '******') or die $!; $smtp->mail('me@gmail.com'); # from addr $smtp->to('me@gmail.com'); $smtp->data(); $smtp->datasend("hey!\n"); $smtp->quit();

Here's the debug output:

Net::SMTP::_SSL>>> Net::SMTP::_SSL Net::SMTP::_SSL>>> IO::Socket::SSL(2.038) Net::SMTP::_SSL>>> IO::Socket::IP(0.37) Net::SMTP::_SSL>>> IO::Socket(1.38) Net::SMTP::_SSL>>> IO::Handle(1.36) Net::SMTP::_SSL>>> Exporter(5.72) Net::SMTP::_SSL>>> Net::SMTP(3.10) Net::SMTP::_SSL>>> Net::Cmd(3.10) Net::SMTP::_SSL=GLOB(0x2af1b78)<<< 220 smtp.gmail.com ESMTP t82sm31059 +92itb.18 - gsmtp Net::SMTP::_SSL=GLOB(0x2af1b78)>>> EHLO local.example.com Net::SMTP::_SSL=GLOB(0x2af1b78)<<< 250-smtp.gmail.com at your service, + [50.66.135.148] Net::SMTP::_SSL=GLOB(0x2af1b78)<<< 250-SIZE 35882577 Net::SMTP::_SSL=GLOB(0x2af1b78)<<< 250-8BITMIME Net::SMTP::_SSL=GLOB(0x2af1b78)<<< 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN- +CLIENTTOKEN OAUTHBEARER XOAUTH Net::SMTP::_SSL=GLOB(0x2af1b78)<<< 250-ENHANCEDSTATUSCODES Net::SMTP::_SSL=GLOB(0x2af1b78)<<< 250-PIPELINING Net::SMTP::_SSL=GLOB(0x2af1b78)<<< 250-CHUNKING Net::SMTP::_SSL=GLOB(0x2af1b78)<<< 250 SMTPUTF8 Net::SMTP::_SSL=GLOB(0x2af1b78)>>> AUTH LOGIN Net::SMTP::_SSL=GLOB(0x2af1b78)<<< 334 VXNlcm5hbWU6 Net::SMTP::_SSL=GLOB(0x2af1b78)<<< (decoded) Username: Net::SMTP::_SSL=GLOB(0x2af1b78)>>> (decoded) me@gmail.com Net::SMTP::_SSL=GLOB(0x2af1b78)>>> c3RldmUuYmVydHJhbmRAZ21haWwuY29t Net::SMTP::_SSL=GLOB(0x2af1b78)<<< 334 UGFzc3dvcmQ6 Net::SMTP::_SSL=GLOB(0x2af1b78)<<< (decoded) Password: Net::SMTP::_SSL=GLOB(0x2af1b78)>>> (decoded) ***** Net::SMTP::_SSL=GLOB(0x2af1b78)>>> OTF4MTY0MzRBaA== Net::SMTP::_SSL=GLOB(0x2af1b78)<<< 235 2.7.0 Accepted Net::SMTP::_SSL=GLOB(0x2af1b78)>>> MAIL FROM:<me@gmail.com> Net::SMTP::_SSL=GLOB(0x2af1b78)<<< 250 2.1.0 OK t82sm3105992itb.18 - g +smtp Net::SMTP::_SSL=GLOB(0x2af1b78)>>> RCPT TO:<me@gmail.com> Net::SMTP::_SSL=GLOB(0x2af1b78)<<< 250 2.1.5 OK t82sm3105992itb.18 - g +smtp Net::SMTP::_SSL=GLOB(0x2af1b78)>>> DATA Net::SMTP::_SSL=GLOB(0x2af1b78)<<< 354 Go ahead t82sm3105992itb.18 - +gsmtp Net::SMTP::_SSL=GLOB(0x2af1b78)>>> hey! Net::SMTP::_SSL=GLOB(0x2af1b78)>>> . Net::SMTP::_SSL=GLOB(0x2af1b78)<<< 250 2.0.0 OK 1475936526 t82sm310599 +2itb.18 - gsmtp Net::SMTP::_SSL=GLOB(0x2af1b78)>>> QUIT Net::SMTP::_SSL=GLOB(0x2af1b78)<<< 221 2.0.0 closing connection t82sm3 +105992itb.18 - gsmtp

Replies are listed 'Best First'.
Re^2: Send email via Gmail
by ultranerds (Hermit) on Oct 08, 2016 at 15:11 UTC
    Ah ok, making some progress here :) The below works:

    use Net::SMTP; my $smtp = Net::SMTP->new('smtp.gmail.com', Hello => 'steampunkjunkies.net', Timeout => 30, Debug => 1, SSL => 1 ) || die "Error: $!"; $smtp->auth($CFG->{db_smtp_user}, $CFG->{db_smtp_pass}) or die "C +ould not authenticate with mail.\n"; $smtp->mail('andy@steampunkjunkies.net'); # from addr $smtp->to('foo@gmail.com'); $smtp->data(); $smtp->datasend("foo\n"); $smtp->quit();


    The problem with that though, is that I actually want to send a fully compiled email already (built up using MIME::Lite). So something like:

    $smtp->data(); $smtp->datasend($body_of_a_full_email_from_MIME_Lite); $smtp->quit();


    Do you understand what I mean?

    Thanks!

      What happens when you try it? What error(s) do you get?

        Aahhh I got it! When building up the original email, I was doing:
        my $msg = MIME::Lite->new( From => $from, To => $to, Type => 'multipart/alternative', Subject => $subject_val, );


        But obviously Net::SMTP does the to/from part itself. Removing it so I just had:

        my $msg = MIME::Lite->new( Type => 'multipart/alternative', Subject => $subject_val, );


        ...and it works like a charm now. Thanks goodness for that!

        Now I can relax a bit for the rest of the weekend haha

        Thanks to both of you :) So for anyone who may come across this post in the future, here is the code I ended up using (appologies for not making it super pretty, but I need to call it a day :))
        my $msg = MIME::Lite->new( Type => 'multipart/alternative', Subject => $subject_val, ); my $att_text = MIME::Lite->new( Type => 'text', Data => "plain text version", Encoding => 'quoted-printable', ); $att_text->attr('content-type' => 'text/plain; charset=UTF-8'); $msg->attach($att_text); my $att_html = MIME::Lite->new( Type => 'text', Data => "<b>html version</b> goo", Encoding => 'quoted-printable', ); $att_html->attr('content-type' => 'text/html; charset=UTF-8'); $msg->attach($att_html); my $email = $msg->as_string(); use Net::SMTP; my $smtp = Net::SMTP->new('smtp.gmail.com', Hello => 'domain.net', Timeout => 30, Debug => 1, SSL => 1 ) || die "Error: $!"; $smtp->auth($CFG->{db_smtp_user}, $CFG->{db_smtp_pass}) or die "Co +uld not authenticate with mail.\n"; $smtp->mail('you@gmail.com'); # from addr $smtp->to('foo@bar.com'); $smtp->data(); $smtp->datasend($email); $smtp->quit();

Log In?
Username:
Password:

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

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

    No recent polls found