Send emails with attachments from R command line

May 30, 2012
By

(This article was first published on W. Andrew Barr's Paleoecology Blog, and kindly contributed to R-bloggers)

The sendmailR package makes it easy to send emails with attachments from the R command line. 

#load package
library("sendmailR")


#use string formatting and your system info to format FROM address
from <- sprintf("<Project1@%s>", Sys.info()[4])
 
to <- "<[email protected]>"
subject <- "Test Email From R"
 
#create list with text of body as first element
#second list element is R object to attach using the mime_part() function
body <- list("Email sent from R. Dataframe attached.",mime_part(data.frame(x=rnorm(1000),y=rnorm(1000)),name="output"))

sendmail(from, to, subject, body, control=list(smtpServer="ASPMX.L.GOOGLE.COM"))

Why would you want to do this, you ask?  Suppose you are doing some lengthy simulations and you need to do other things while they are running.  Just code up an email to be sent alerting you when they are finished. 

It is also a nice way to export data computed during an R session by sending it to your webmail account.  You can change the from address from Project1 (as in example code above) to something more meaningful for a specific project, then set up an email filter to organize your output emails.  The useful possibilities are numerous.

To leave a comment for the author, please follow the link and comment on his blog: W. Andrew Barr's Paleoecology Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.