Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Sending mails via gmail

by morgon (Priest)
on Aug 30, 2016 at 20:57 UTC ( [id://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:

Net::SMTP::TLS
Email::Send::SMTP::TLS
Email::Send::Gmail
Email::Send::SMTP::Gmail

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 http://stackoverflow.com/a/33244509/2173773 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 =>'smtp.gmail.com', -login =>'hakon.hagland@gmail.com', -pass =>'?????' ); die "session error: $error" if $mail ==-1; $mail->send( -to =>'hakon.hagland@gmail.com', -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 =>'smtp.gmail.com', -login =>'myemail@gmail.com', -pass =>'mypassword' ); die "session error: $error" if $mail ==-1; <> $mail->send( -to =>'myemail@gmail.com', -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?
Username:
Password:

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

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

    No recent polls found