Sending email in R via outlook

user3022875 picture user3022875 · Nov 8, 2014 · Viewed 33.8k times · Source

How can I send emails in R via Outlook?

All the examples of sendmailR use the gmail server, but I cannot do that.

Answer

agstudy picture agstudy · Nov 8, 2014

You can use RDCOMClient package to access to COM objects from within R. You can easily access the Application Object (Outlook) and configure it. Here a simple example of sending an email:

library(RDCOMClient)
## init com api
OutApp <- COMCreate("Outlook.Application")
## create an email 
outMail = OutApp$CreateItem(0)
## configure  email parameter 
outMail[["To"]] = "[email protected]"
outMail[["subject"]] = "some subject"
outMail[["body"]] = "some body"
## send it                     
outMail$Send()

Of course, this assumes that you have already install outlook and configure it to send/receive your emails. Adding attachment is quite simple also:

outMail[["Attachments"]]$Add(path_to_attch_file)

sending on behalf of secondary mailbox:

outMail[["SentOnBehalfOfName"]] = "[email protected]"