Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Sending mails via gmail

by morgon (Priest)
on Aug 30, 2016 at 20:57 UTC ( #1170831=perlquestion: print w/replies, xml ) Need Help??

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

Hi all,

I just realize that I am not up to date with emails in Perl anymore.

Last time I wanted to programatically to send an email I was using MIME::Lite, but that won't impress the kids today...

What I want to do is to send mails using my gmail-account and I am bit baffled so see that there is plethora of options:


So as I am too lazy to read lots of reviews my question is:

Is there currently a canonical/best/preferred way to send mails via gmail or are all the different ways equally valid options and it only comes down to taste?

Many thanks!

Replies are listed 'Best First'.
Re: Sending mails via gmail
by RonW (Parson) on Aug 30, 2016 at 23:17 UTC

    You can use any of those (and others), but Email::Send::Gmail looks very easy to use.

    I'd say look at the usage examples then pick one you like.

    Update: Email::Send::SMTP::Gmail looks like it might be a better choice as it appears to have been updated 2016-Oct-10 (as of this update to this post).

Re: Sending mails via gmail
by noxxi (Pilgrim) on Sep 03, 2016 at 07:53 UTC
    Just to compare the modules you've proposed:
    • Net::SMTP::TLS - based on Net::SMTP, last update 2006. Will not work without bug fixes.
    • These bug fixes are included in the successor Net::SMTP::TLS_ButMaintained, which isn't maintained anymore too (last release 2013). Apart from that this module disables certificate validation and this way opens you to man in the middle attacks.
    • Email::Send::SMTP::TLS - just uses Net::SMTP::TLS_ButMaintained and thus has the same problems (no certificate validation)
    • Email::Send::Gmail - based on Net::SMTP::SSL which is just a hack into Net::SMTP (changes superclass to IO::Socket::SSL). Not maintained anymore, i.e. last update 2008.
    • Email::Send::SMTP::Gmail - based on Net::SMTPS which again is based on Net::SMTP. Email::Send::SMTP::Gmail disables certificate validation by default too.
    My recommendation:
    go with a recent version of Net::SMTP. This version has support for explicit and implicit SSL/TLS and also for IPv6. It also does proper certificate validation and validation is enabled by default. And it is a CORE module, although the version with SSL is only included starting with perl 5.22.0 (and still needs IO::Socket::SSL installed). With older versions you can either install the new libnet (which includes Net::SMTP), install Net::SSLGlue::SMTP which monkey patches Net::SMTP for SSL support or use Net::SMTPS which behaves similar (but not the same) to Net::SMTP with SSL support.
    Since Net::SMTP cares only about the SMTP dialog you can still use Mime::Lite as you've used to be to construct the mail and then send it within the SMTP dialog with $smtp->data($mime_lite->as_string).

      Thanks for the detailed & very considerate reply (not OP).

Re: Sending mails via gmail
by Anonymous Monk on Aug 30, 2016 at 23:51 UTC

    If you are too lazy to look up information, go with Net::* module. It will talk directly to Google Mail SMTP server, which will remove your local set up in case you would need to debug things.

Re: Sending mails via gmail
by hakonhagland (Scribe) on Sep 02, 2016 at 22:17 UTC
    I tried Email::Send::SMTP::Gmail from Ubuntu 16.04, and it worked after some tweaking. First I needed to change the layer from the default tls to ssl, then the port from the default 25 to 465. After that, I needed to change my gmail account settings to allow access for less secure apps. See this post for more information.
    use strict; use warnings; use Email::Send::SMTP::Gmail; my ($mail,$error) = Email::Send::SMTP::Gmail->new( -layer =>'ssl', -port =>'465', -smtp =>'', -login =>'', -pass =>'?????' ); die "session error: $error" if $mail ==-1; $mail->send( -to =>'', -subject =>'Hello!', -body =>'Just testing it', -attachments=>'test.pdf' ); $mail->bye;

      AWESOME !!! THIS REALLY WORKS, and sends the attachment. THANKS!!!!

      use strict; use warnings; use Email::Send::SMTP::Gmail; my ($mail,$error) = Email::Send::SMTP::Gmail->new( -layer =>'ssl', -port =>'465', -smtp =>'', -login =>'', -pass =>'mypassword' ); die "session error: $error" if $mail ==-1; <> $mail->send( -to =>'', -subject =>'Hello!', -body =>'Just testing it', -attachments=>'C:\Users\MyName\Documents\My_IT_Resume.doc' ); $mail->bye;
Re: Sending mails via gmail
by $h4X4_&#124;=73}{ (Monk) on Aug 31, 2016 at 08:37 UTC

    You can "Send" email from any one of those modules with a gmail account. Some of those Gmail modules are to read/get emails from gmail, because gmail has a different way to retrieve new email.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2022-12-01 13:30 GMT
Find Nodes?
    Voting Booth?