Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send an email with attachment from R in windows

Tags:

email

r

sendmailr

I have a scheduled an R script running from a windows machine.

After it finishes, I wish this script to automatically send an email with some log file attached.

Using shell() with some other scripts may be possible, but I was wondering if there is a better solution within R. Thanks.

like image 490
ahala Avatar asked May 21 '10 21:05

ahala


People also ask

Can R send an email?

R has a package for sending emails from Microsoft Outlook as well.

How do I send an attachment in Windows?

Attach a file to a messageCreate a message, or for an existing message, click Reply, Reply All, or Forward. In the message window, click MESSAGE. In the Include group, click Attach File. Browse to click the file that you want to attach, and then click Insert.


2 Answers

sendmailR works for me on Windows 7. I referenced http://cran.es.r-project.org/web/packages/sendmailR/sendmailR.pdf

smtpServer= info for Outlook 2010 is in File -> Account Settings -> Account Settings -> double click your account -> text in "Server" box

library(sendmailR)  #set working directory setwd("C:/workingdirectorypath")  #####send plain email  from <- "[email protected]" to <- "[email protected]" subject <- "Email Subject" body <- "Email body."                      mailControl=list(smtpServer="serverinfo")  sendmail(from=from,to=to,subject=subject,msg=body,control=mailControl)  #####send same email with attachment  #needs full path if not in working directory attachmentPath <- "subfolder/log.txt"  #same as attachmentPath if using working directory attachmentName <- "log.txt"  #key part for attachments, put the body and the mime_part in a list for msg attachmentObject <- mime_part(x=attachmentPath,name=attachmentName) bodyWithAttachment <- list(body,attachmentObject)  sendmail(from=from,to=to,subject=subject,msg=bodyWithAttachment,control=mailControl) 

In addition, multiple files can be sent by adding another mime_part to the msg list as follows (I also condensed it):

attachmentObject <- mime_part(x="subfolder/log.txt",name="log.txt") attachmentObject2 <- mime_part(x="subfolder/log2.txt",name="log2.txt") bodyWithAttachment <- list(body,attachmentObject,attachmentObject2) 
like image 95
ARobertson Avatar answered Sep 22 '22 08:09

ARobertson


Use mailR - it works with authentication, attachments, it automatically send txt message along with html and more.

mailR requires rJava which can be a bit of a pain sometimes. On windows I haven't had any problems. On ubuntu this solved the one issue I've had:

sudo apt-get install openjdk-jdk  

in R

install.packages("devtools", dep = T) library(devtools) install_github("rpremraj/mailR") 

(if you have trouble with rJava - try sudo R CMD javareconf in terminal)

mailR is easy to work with and well documented on the github page.

Example from the documentaion

library(mailR) send.mail(from = "[email protected]",           to = c("[email protected]", "[email protected]"),           subject = "Subject of the email",           body = "Body of the email",           smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "gmail_username", passwd = "password", ssl = TRUE),           authenticate = TRUE,           send = TRUE,           attach.files = c("./download.log", "upload.log", "https://dl.dropboxusercontent.com/u/5031586/How%20to%20use%20the%20Public%20folder.rtf"),           file.names = c("Download log.log", "Upload log.log", "DropBox File.rtf"), # optional parameter           file.descriptions = c("Description for download log", "Description for upload log", "DropBox File"), # optional parameter           debug = TRUE) 

Note: your smtp server might find excessive use suspicious. This is the case with e.g. gmail. So after sending a few mails you probably have to log in to the gmail account and see if the account has been temporarily disabled. Also note that if you use a gmail account with two-factor authentication you need to use an application specific password.

like image 38
Andreas Avatar answered Sep 19 '22 08:09

Andreas