The mailR package allows you to easily send e-mails with R, but you need the right mailR SMTP settings. Getting the SMTP settings just right to establish a connection to e-mail hosts like Gmail, Outlook, or Yahoo can be challenging. This is especially true when there are some settings you need to change on the host side to make everything work. In this post, I'll show you how to connect to the major e-mail providers such as Gmail, Outlook, Yahoo, Hotmail, and AOL. For each host, you'll get the SMTP host name, ports and login requirements. In addition, I'll show you how to deal with STARTTLS, the default secure communications protocol used by many major e-mail providers for SMTP. Incidentally STARTTLS is not supported by mailR as of (v0.4.1). To get mailR to work with these services, you'll need to alter your account settings to allow communication with “less secure apps”.
Major Webmail Hosts
If you want to send e-mails with R, and you use a webmail provider this article should get you moving in the right direction. To start, you need to know that outgoing e-mail is handled by SMTP servers. SMTP services are typically hosted by your internet service provider, website host, or other web-based services like search engines. While I can't cover all cases, this article will cover the generic mailR setup for SSL and TLS SMTP settings. SSL and TLS are secure e-mail transfer protocols. After discussing the general SMTP setup, we'll cover how to setup mailR for the big 5 webmail providers.
Generic mailR SMTP Settings (SSL and TLS)
To send e-mails with the mailR package, you use the send.mail() command. Here is a generic example:
require(mailR) send.mail( from="[from email address]", to = "[to email address] ", subject = "The Subject line", body = "The message you want to send", smtp = list(host.name = "some.smtp.server.com", port = [PortNumber], user.name = "[from email address]", passwd = "[YourPassword]", #ssl = T #tls = T), authenticate = T, send = T )
Looking over the code, you see that several details are needed to send an email. Most important to our discussion are the SMTP settings starting on line 6. Usually you can find the SMTP details by searching for “SMTP and [whoever is hosting your mail service….]. Here is a quick summary of the most important arguments, you need:
- host.name : string, the name of the server you are trying to connect to (e.g., smtp.gmail.com)
- port : number, generally 465 (SSL) or 587 (TLS)
- user.name: string (email address), most often this is the e-mail address you are sending from.
- passwd: string, the password you use to sign into the mail server
- Security arguments TLS or SSL: If you choose the incorrect security protocol, the SMTP server may deny your request. Most of the major webmail providers support TLS.
In my experience, establishing communication with an SMTP server is either SSL or TLS. If you are using SSL, the port number is usually 465 and for TLS the port is usually 587. Even so, it's very easy to get hung up on the server details… Especially, when you try both and it doesn't work. This is most common with the major webmail services (e.g, Gmail, Yahoo, etc), because they use a different protocol by default, STARTTLS. STARTTLS isn't supported by mailR but there are options. More on this below.
Connecting to the Major Webmail Services (Gmail, Outlook, Yahoo, etc.)
So, who are the major webmail services? My determination of the big 5 e-mail services was not particularly scientific. The best piece of data I found reporting the proportion of e-mail domains used was "List of most popular email domains” from 2016. Along with some other searches, it looks like the big 5 e-mail providers were “webmail” providers. Here is the shortlist
As mentioned in the previous section, some of these providers use STARTTLS. If they do, all your SMTP settings could be correct in your send.mail() command, and your connection will still be rejected by the SMTP server. In the table below, I've summarized the connection details for the major webmail providers and indicated which use STARTTLS.
|Provider||host.name||port||SSL or TSL||Uses STARTTLS||R Snippet|
|ionos (my webhost)||smtp.ionos.com||587||TLS||NO||Code|
Now, just because Gmail uses STARTTLS doesn't mean you can't use mailR. You can. But, there is an extra step required, and we'll discuss that in the next section.
Dealing with STARTTLS
Great, so you've got your sever details all set but the connection isn't working. The issue is likely that your host is using a STARTTLS security protocol. As of mailR (v0.4.1) STARTTLS isn't supported. One way to overcome the issue is to use your e-mail host's “less secure apps” option. Warning: using the “less secure apps” option will lessen your account security. So, change this setting with caution.
Below is a quick summary of the types of errors I've encountered with Gmail, Yahoo, and AOL due to STARTTLS. Also, I've provided you with a direct link to the page where you can enable the “less secure apps” option, and some visuals describing where to make the necessary change.
|Link to set less secure apps option: https://myaccount.google.com/security|
|Link to set less secure apps option: https://login.yahoo.com/account/security|
|Link to set less secure apps option: https://login.aol.com/account/security|
Once you've enabled the “less secure apps” setting, try to run your mailR script again. It should work this time, assuming all your settings are correct.
Engineering Note on STARTTLS Issue
While the process mentioned above resolves the SMTP communication issues with these hosts, we are treating a symptom rather than the root cause. The best resolution is to have the STARTTLS option available in the mailR package. While I've not dug into the code behind the mailR package, it seems to be built on top of javaMail. And, JavaMail appears to have STARTTLS capabilities as suggested by this stackoverflow thread: https://stackoverflow.com/questions/5592112/javamail-smtp-properties-for-starttls. So, there is definitely an opportunity for improvement.
During the preparation of this article, I setup a Hotmail account and tried to send an e-mail using the mailR package. I got an error on the first attempt that wasn't STARTTLS related. It looked like this:
To solve this issues, I needed to send an e-mail from the Hotmail web interface to verify that I was not a robot. After I sent a test message, everything worked fine with mailR.
For the last month or so, I've been working on the mobileTrigger package which provides a framework to interact with R scripts, reports, and models using your e-mail client. It is still a work in progress but essentially it's a functional extension of my previous post “Running Remote R Scripts with Mobile Devices using E-mail Triggers”. While developing the package, it became apparent that many of the webmail hosts didn't play nicely with R's mailR package out of the box. To that end, the goal of this article is to provide a resource to folks who'll want to use the mobileTrigger package and not waste lots of time struggling with their SMTP settings.
In this article, we learned how to get the mailR package to work with some of the major webmail providers including, Gmail, Outlook, and Yahoo. For each of these providers, the critical SMTP parameters in mailR's send.mail() command was given. You also learned that while all your SMTP settings may be correct, communications with the SMTP server can still be revoked if the provider uses the STARTTLS protocol. Examples of webmail providers that use STARTTLS included Gmail, Yahoo, and AOL. We wrapped up by showing how to get around this issue by changing the host-side settings concerning “less secure apps”.
Other Articles at r-bar.net
Running Remote R Scripts with Mobile Devices using E-mail Triggers
Super Dark Mode Theme for R-Studio
Learning and Teaching R | Get to the plot
WooCommerce Image Gallery | Step by Step, Automate with R
XmR Chart | Step-by-Step Guide by Hand and with R
The post MailR SMTP Setup (Gmail, Outlook, Yahoo) | STARTTLS appeared first on R-BAR.