In R, there is a package named mailR that allows you to send emails from R. In this tutorial, I will show you how to send email using gmail and MS Outlook.
library(mailR)
send.mail(from="sandy.david@gmail.com",
to="deepanshu.bhalla@outlook.com",
subject="Test Email",
body="PFA the desired document",
html=T,
smtp=list(host.name = "smtp.gmail.com",
port = 465,
user.name = "sandy.david@gmail.com",
passwd = "xxxxxxxxx",
ssl = T),
authenticate=T,
attach.files="C:\\Users\\Deepanshu\\Downloads\\Nature of Expenses.xls")
Detailed Explanation
- In "from =" option, you can enter email address using which you want to send email.
- In "to=" option, you need to specify email address of the recipient. Similarly, you can enter your desired subject line and body of the email.
- Keep host.name and port under "smtp=" parameter as it is. Don't change anything. Enter your user.name and password of the gmail account you want to use to send email.
- Under attach.files= option, you can mention path of the file you want to attach in the email.
You can add multiple recipients including the following code.
to = c("Recipient 1 <recipient1@gmail.com>", "recipient2@gmail.com"),
cc = c("CC Recipient <cc.recipient@gmail.com>"),
bcc = c("BCC Recipient <bcc.recipient@gmail.com>")
R : Send email using MS Outlook
In enterprise environment, we generally send email using Microsoft (MS) Outlook. You can use RDCOMClient package to complete this task.
How to install RDCOMClient package
# For R Version (R >= 4.0.x) library(remotes) remotes::install_github("BSchamberger/RDCOMClient") # For R Version (R >= 3.5) install.packages("RDCOMClient", repos = " http://www.omegahat.net/R") # For R Version (R <= 3.4.4) install.packages("RDCOMClient", repos = " http://www.stats.ox.ac.uk/pub/RWin")
library("RDCOMClient")Send HTML Email Message
OutApp <- COMCreate("Outlook.Application")
## create an email
outMail = OutApp$CreateItem(0)
## configure email parameter
outMail[["To"]] = "abc@gmail.com"
outMail[["subject"]] = "Test Email"
outMail[["body"]] = "Hi, How are you?"
## send it
outMail$Send()
The main advantage of HTML email is that it supports various formatting options. For example, it supports line break, font color etc.
Replace outMail[["body"]] = "Hi, How are you?" with the following code
bd = paste("Hi,", "<br>", "Output File has been saved on location")You can also send attachments using the following syntax
outMail[["HTMLbody"]] = bd
outMail[["Attachments"]]$Add("C:/Users/bhalla/sample.xlsx")
Good one
ReplyDeletewow, it is the edge of R ("era eRa" - in Polish) :)
ReplyDeletegmail doesnt allow any non gmail app to send email.
ReplyDeleteIn my case it is not working
Nice article ...one question ,can we attach outlook signature in the email created by the R program?...If yes,the how?
ReplyDeleteIs this secure?
ReplyDeleteCan we send file data as table and text message both in mail using R. The body part either sends message or table.
ReplyDeleteIncase you get any error:
ReplyDelete1. Enable access to 'less secure apps' here:
http://www.google.com/settings/security/lesssecureapps
2. Disable two step authentication
Getting an Error, Plese help me
ReplyDeletecould not find function "send.mail"
continue getting error "could not find function "send.mail"
ReplyDeletehow to sent mail with multiple attachment.
ReplyDeleteattach.files = c( "E:\\July Agent Data\\IIFL\\IIFL Agent_data_2019-07-24.csv",
Delete"E:\\IIFL\\Stats\\Chat Disposition Comment 2019-07-24.csv",
"E:\\IIFL\\Stats\\Chat Disposition Reason 2019-07-24.csv",
"E:\\IIFL\\Stats\\Chat Disposition Sub Reason 2019-07-24.csv",
"E:\\IIFL\\Stats\\User Feedback 2019-07-24.csv"),
while installing the RDCOMClient it says --> package ‘RDCOMClient’ is not available (for R version 3.6.2)
ReplyDeletegetting below error :
ReplyDeleteError in .jnew("org.apache.commons.mail.HtmlEmail") :
java.lang.NoClassDefFoundError: javax/activation/DataSource
not work in my side i got this error EmailException (Java): Sending the email to the following server failed : smtp.gmail.com:465
ReplyDelete