Sending Email from R (using sendEmail)

November 25, 2011

(This article was first published on indiacrunchin » R, and kindly contributed to R-bloggers)

Like a lot of other R users I’ve felt the need for sending email from R. I haven’t surveyed CRAN for such a package but looked for the possibility of sending command line email in Windows.

Found a nice application called sendEmail that can be found here

Below are code snippets in R that will allow you to make use of this application.

First, download the application from the link provided above. Get the TLS supported variant. Unzip and add to your PATH variable, the location of the sendEmail executable file.

Create a text file with some sample content, place it in C:/ and name it “filetest.txt”. This is the file used as a sample attachment in the code below.

I am assuming you’re using Gmail and have a username: [email protected] with password: xyz.
Please make appropriate changes to test for your credentials.

In the sendEmail folder, README.txt provides the necessary information of the command line parameters (see section ‘Usage Overview’). The code snippet below simply creates the relevant parameter-value combination(s) and uses system() to issue a system call.

# Create the required command line parameters for sendEmail to work
paramsList <- list()
paramsList$fromAddress <- c("-f",'[email protected]')
paramsList$toAddress <- c("-t",'[email protected]')
paramsList$emailSubject <- c("-u","Test Email from R")
paramsList$listemailMessage <- c("-m",paste("Sent at ",format(Sys.time(),"%Y-%d-%m:%H-%M-%S"),sep=" "))
paramsList$serverAndPort <- c("-s","")
paramsList$fileAttachPath <- c("-a","C:/filetest.txt")
paramsList$accUsername <- c("-xu","[email protected]")
paramsList$accPassword <- c("-xp","xyz")

# Add double quotes to the parameter values
paramsList1 <- lapply(paramsList,function(x){x[2] <- dQuote(x[2]);paste(x,collapse = " ")})

# Combine to create one single function call
suffixCall <- paste("c",paramsList1),collapse = " ")
commandCall <- paste("sendEmail",suffixCall,sep = " ")
# Issue the command via system() – sending it to CMD
returnVal <- system(commandCall,intern=T,wait=T)

You could use the R2HTML package in R to send model summary report in HTML like below:

# ———
# From the help files of R2HTML package
tmpfic <- HTMLInitFile(outdir="C:/",filename="regressionTest")
HTML(as.title("Regression Example"),file=tmpfic)
HTML(summary(lm(Sepal.Length~Sepal.Width+ Petal.Length + Petal.Width,data=iris)),file=tmpfic)
browseURL(tmpfic) # View the file in your local browser

# Update the paramsList appropriately
paramsList$fileAttachPath <- c("-a",tmpfic)

# Recreate the new command and execute
paramsList1 <- lapply(paramsList,function(x){x[2] <- dQuote(x[2]);paste(x,collapse = " ")})
suffixCall <- paste("c",paramsList1),collapse = " ")
commandCall <- paste("sendEmail",suffixCall,sep = " ")
returnVal <- system(commandCall,intern=T,wait=T)

The solution presented here is not a pure R implementation and could be considered more of a hack.
Thanks to Brandon Zehm for creating sendEmail. More information about Brandon Zehm can be found here

To leave a comment for the author, please follow the link and comment on their blog: indiacrunchin » R. 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.


Mango solutions

RStudio homepage

Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training


CRC R books series

Contact us if you wish to help support R-bloggers, and place your banner here.

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)