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

In reply to Re: Send email via Gmail by stevieb
in thread Send email via Gmail by ultranerds

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.