MailR SMTP Setup (Gmail, Outlook, Yahoo) | STARTTLS

[This article was first published on R – R-BAR, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Logos of various webmail hosts, mailR SMTP settingsThe 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:

  1. host.name : string, the name of the server you are trying to connect to (e.g., smtp.gmail.com)
  2. port : number, generally 465 (SSL) or 587 (TLS)
  3. user.name: string (email address), most often this is the e-mail address you are sending from.
  4. passwd: string, the password you use to sign into the mail server
  5. 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

  • Gmail
  • Outlook
  • Hotmail
  • AOL
  • Yahoo

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
gmail smtp.gmail.com 587 TLS YES Code
Outlook smtp.office365.com 587 TLS NO Code
Yahoo smtp.mail.yahoo.com 587 TLS YES Code
Hotmail smtp.live.com 587 TLS NO Code
AOL smtp.aol.com 587 TLS YES Code
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.

Gmail

mailR(javamail) ERRORS: 
[1] org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.Gmail.com:587

[2] Caused by: javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8  https://support.google.com/mail/?p=BadCredentials c21sm18730030qkm.8 - gsmtp

[3] Caused by: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. x14sm29391461qtc.44 - gsmtp

Link to set less secure apps option: https://myaccount.google.com/security
gmail less safe apps security screen

Yahoo

mailR(javamail) ERRORS: 
[1] org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.mail.yahoo.com:587

[2] Caused by: javax.mail.AuthenticationFailedException: 535 5.7.0 (#AUTH005) Too many bad auth attempts.

Link to set less secure apps option: https://login.yahoo.com/account/security
yahoo mail less safe apps security screen

AOL

mailR(javamail) ERRORS: 
[1] org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.aol.com:587

[2] Caused by: javax.mail.AuthenticationFailedException: 535 5.7.0 (#AUTH005) Too many bad auth attempts.

Link to set less secure apps option: https://login.aol.com/account/security
yahoo mail less safe apps security screen

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.

Hotmail

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:

mailR(javamail) ERRORS: 
[1] com.sun.mail.smtp.SMTPSendFailedException: 554 5.2.0 STOREDRV.Submission.Exception:OutboundSpamException; Failed to process message due to a permanent exception with message WASCL UserAction verdict is not None. Actual verdict is HipNotify, ShowTierUpgrade. OutboundSpamException: WASCL UserAction verdict is not None. Actual verdict is HipNotify, ShowTierUpgrade. [Hostname=BYAPR15MB3303.namprd15.prod.outlook.com]

[2] org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.live.com:587

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.

Context

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.

Summary

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.

To leave a comment for the author, please follow the link and comment on their blog: R – R-BAR.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)