Send emails with attachments from R command line

May 30, 2012

(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

#use string formatting and your system info to format FROM address
from <- sprintf(“”,[4])
to <- “
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 their blog: W. Andrew Barr's Paleoecology Blog. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, 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.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)