Sending email through Perl can be done in many ways. I like to simply call sendmail, but that approach is not always portable between operating systems. For that, we use Net::SMTP. But if you plan to send email via Gmail, normal plain ol’ port 25 SMTP won’t work. For that, we need to switch to TLS/SSL with Authentication.
But first, let’s review what the Gmail settings are.
- Server : smtp.gmail.com
- Port : 587 for TLS, 465 for SSL
- Username : Your full gmail account
- Password : Your gmail password
When using Net::SMTP::TLS, I had this error message:
invalid SSL_version specified at /usr/share/perl5/IO/Socket/SSL.pm line 575.
This happens on Ubuntu with IO::Socket::SSL at version 2.024 and Net::SMTP::TLS at version 0.12. I do not have a solution for this, but there appears to be some incompatibility with these versions. Net::SMTP::TLS was installed via apt-get.
I then reverted to Net::SMTP::SSL, but it did not work. In fact, I got an error message emailed to me in Gmail. It read as follow:
Someone just tried to sign in to your Google Account firstname.lastname@example.org from an app that doesn’t meet modern security standards.
We strongly recommend that you use a secure app, like Gmail, to access your account. All apps made by Google meet these security standards. Using a less secure app, on the other hand, could leave your account vulnerable. Learn more.
Google stopped this sign-in attempt, but you should review your recently used devices.
Typically, the error message doesn’t really tell me what I did wrong. After a bit of digging, I found some articles explaining that Gmail is now requiring Oath 2.0 authentication, which sadly, Net::SMTP::SSL does not provide.
There is a solution. You can change your Gmail settings to allow a less-secure login. After I made this change, the module worked beautifully. Ideally, you should be using Oath 2.0 instead.