Send Email Using R-Program

Using mailR & GMail

Image for post
Image for post

To send email using the R programming language we will need to install the “mailR” package and have a GMail account from Google. We will also use Googles smtp server to send the message.

In this article I assume you already have R and RStudio installed on your computer, and a Gmail account if you do not already have a Gmail account, you can sign up here. If you do not already have R and RStudio installed I have another article showing you the easy steps to do so here.

If you do not already have “mailR” installed, run the following in your RStudio Console.

install.packages("mailR")

Use the code below either as a R script or run it in the RStudio console to send email. Just replace the ‘your_email’ value with your actual Gmail account, the ‘recipient_email’ with your recipient email address and ‘your_email_password’ with the password of your Gmail account.

#install.packages("mailR")
library(mailR)
sender <- "your_email@gmail.com"
recipients <- c("recipient_email@gmail.com")
send.mail(from = sender,
to = recipients,
subject = "Test mail from Rstudio",
body = "Test email body",
smtp = list(host.name = "smtp.gmail.com", port = 465,
user.name = "your_email@gmail.com",
passwd = "your_email_password", ssl = TRUE),
authenticate = TRUE,
send = TRUE)

You may get the following error message

org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:465
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1410)
at org.apache.commons.mail.Email.send(Email.java:1437)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at RJavaTools.invokeMethod(RJavaTools.java:386)
Caused by: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbuu
534-5.7.14 XIuKGOx55rNHFSvDWkzr2B1augfG-Z1zGTYEQ9IZ-iD_aDivhY0aUWNjt0C4ckYrqyx0WS
534-5.7.14 Zbz-1z65Xz8C1irD5crNwDSpgL3qQGgo6V-PvkiA6h1on6Hnn-YjSeho0uBWcG59x1C4TH
534-5.7.14 F6ZM2i2TToB9tIjzoUv_3encd3EW0Aw04XBaTVkoWseyUOaTTvglnCIj23ZNrIncqXW0f5
534-5.7.14 gjMKNtrSAOBmJxrgnXGrRS2BC9y9Q> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/answer/78754 t62sm25604268pfa.38 - gsmtp

at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:892)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:814)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:728)
at javax.mail.Service.connect(Service.java:386)
at javax.mail.Service.connect(Service.java:245)
at javax.mail.Service.connect(Service.java:194)
at javax.mail.Transport.send0(Transport.java:253)
at javax.mail.Transport.send(Transport.java:124)
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1400)
... 6 more
Error: EmailException (Java): Sending the email to the following server failed : smtp.gmail.com:465

To fix it you may need to enable access to ‘less secure apps’ here:
http://www.google.com/settings/security/lesssecureapps

Image for post
Image for post

Thanks for reading this article I hope its helpful to you all ! Keep up the learning, and if you would like math, computer science, programming and algorithm analysis videos please visit and subscribe to my YouTube channels (randerson112358 & compsci112358 )

Check Out the following for content / videos on Computer Science, Algorithm Analysis, Programming and Logic:

YouTube Channel:
randerson112358: https://www.youtube.com/channel/UCaV_0qp2NZd319K4_K8Z5SQ

compsci112358:
https://www.youtube.com/channel/UCbmb5IoBtHZTpYZCDBOC1CA

Website:
http://everythingcomputerscience.com/

Video Tutorials on Recurrence Relation:
https://www.udemy.com/recurrence-relation-made-easy/

Video Tutorial on Algorithm Analysis:
https://www.udemy.com/algorithm-analysis/

Twitter:
https://twitter.com/CsEverything

YouTube Channel:

Image for post
Image for post

Computer Science Website:

Image for post
Image for post

Udemy Videos on Algortithm Analysis:

Image for post
Image for post

RESOURCES:

https://stackoverflow.com/questions/37343074/error-in-sending-email-through-gmail-by-using-mailr

https://myaccount.google.com/u/1/lesssecureapps?pageId=none

https://www.rdocumentation.org/packages/mailR/versions/0.4.1

https://www.listendata.com/2015/12/send-email-from-r.html

https://stackoverflow.com/questions/29450427/sending-mail-from-r-mailr

https://www.digitalocean.com/community/tutorials/how-to-use-google-s-smtp-server
https://www.siteground.com/kb/google_free_smtp_server/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store