Send Email from R

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.

R Code : Send Email from R using gmail account
subject="Test Email",
body="PFA the desired document",
smtp=list( = "",
port = 465, = "",
passwd = "xxxxxxxxx",
ssl = T),
attach.files="C:\\Users\\Deepanshu\\Downloads\\Nature of Expenses.xls")

Detailed Explanation
  1. In "from =" option, you can enter email address using which you want to send email.
  2. 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.
  3. Keep and port under "smtp=" parameter as it is. Don't change anything. Enter your and password of the gmail account you want to use to send email.
  4. 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 <>", ""),
cc = c("CC Recipient <>"),
bcc = c("BCC Recipient <>")

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 >= 3.5)
install.packages("RDCOMClient", repos = "")

# For R Version (R <= 3.4.4)
install.packages("RDCOMClient", repos = "")
OutApp <- COMCreate("Outlook.Application")

## create an email
outMail = OutApp$CreateItem(0)

## configure email parameter
outMail[["To"]] = ""
outMail[["subject"]] = "Test Email"
outMail[["body"]] = "Hi, How are you?"

## send it
Send HTML Email Message

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")
outMail[["HTMLbody"]] = bd
You can also send attachments using the following syntax
Related Posts
About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he has worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and Human Resource.

13 Responses to "Send Email from R"
  1. wow, it is the edge of R ("era eRa" - in Polish) :)

  2. gmail doesnt allow any non gmail app to send email.
    In my case it is not working

  3. Nice article question ,can we attach outlook signature in the email created by the R program?...If yes,the how?

  4. Can we send file data as table and text message both in mail using R. The body part either sends message or table.

  5. Incase you get any error:
    1. Enable access to 'less secure apps' here:

    2. Disable two step authentication

  6. Getting an Error, Plese help me

    could not find function "send.mail"

  7. continue getting error "could not find function "send.mail"

  8. how to sent mail with multiple attachment.

    1. attach.files = c( "E:\\July Agent Data\\IIFL\\IIFL Agent_data_2019-07-24.csv",
      "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"),

  9. while installing the RDCOMClient it says --> package ‘RDCOMClient’ is not available (for R version 3.6.2)

  10. getting below error :
    Error in .jnew("org.apache.commons.mail.HtmlEmail") :
    java.lang.NoClassDefFoundError: javax/activation/DataSource


Next → ← Prev
Love this Post? Spread the Word!