Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

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","smtp.gmail.com:587") 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(do.call("c",paramsList1),collapse = " ")
commandCall <- paste("sendEmail",suffixCall,sep = " ")
# Issue the command via system() – sending it to CMD
returnVal <- system(commandCall,intern=T,wait=T)
print(returnVal)

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

# ———
library(R2HTML)
data(iris)
# 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)
HTMLEndFile(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(do.call("c",paramsList1),collapse = " ")
commandCall <- paste("sendEmail",suffixCall,sep = " ")
returnVal <- system(commandCall,intern=T,wait=T)
print(returnVal)

The solution presented here is not a pure R implementation and could be considered more of a hack.