Sending Email from R (using sendEmail)

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: xyz@gmail.com 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",'xyz@gmail.com')
paramsList$toAddress <- c("-t",'xyz@gmail.com')
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","xyz@gmail.com")
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.
Thanks to Brandon Zehm for creating sendEmail. More information about Brandon Zehm can be found here

About these ads

About indiacrunchin
Any sufficiently advanced technology is indistinguishable from magic -- Arthur C. Clarke

4 Responses to Sending Email from R (using sendEmail)

  1. Tal Galili says:

    I LOVE your hack – thank you for this post :)

  2. Olaf Mersmann says:

    Take a look at the sendmailR package on CRAN.

    • heuristicandrew says:

      The advantage of sendMail over sendmailR is that sendMail works with hosts that do not have local SMTP setup. It seems like a good solution to use GMail from Amazon EC2.

  3. Jussi says:

    # This worked for me
    paramsList <- list()
    paramsList$fromAddress <- c("-f",'sender@gmail.com')
    paramsList$toAddress <- c("-t",'recipient@welho.com','jtuulisaari@gmail.com')
    paramsList$emailSubject <- c("-u","Email from Kattila")
    paramsList$listemailMessage <- c("-m",paste("Sent at ",format(Sys.time(),"%Y-%d-%m:%H-%M-%S"),sep=" "))
    paramsList$serverAndPort <- c("-s","smtp.gmail.com:25")
    #paramsList$serverAndPort <- c("-s","smtp.gmail.com:587")

    # paramsList$fileAttachPath <- c("-a","C:/filetest.txt")
    paramsList$accUsername <- c("-xu","sender")
    paramsList$accPassword <- c("-xp","passwd")
    paramsList1 <- paramsList;
    paramsList1$listemailMessage[2] <- dQuote(paramsList1$listemailMessage[2])
    paramsList1$emailSubject[2] <- dQuote(paramsList1$emailSubject[2])
    suffixCall <- paste(unlist(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)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: