Saturday, February 15, 2014

Sending email from R using Gmail

As mentioned previously, the sendmailR package generally works well for sending an email from within R. When an ISP blocks traffic on port 25, however, sendmailR cannot be used unless a a local mailserver is configured to act as a relay. This means that sendmailR is unable to reliably sending email on arbitrary machines and across arbitrary network connections.

There is gmailR, of course, but it requires rJython, and using Python through R via Java is just too many levels of indirection for something as simple as sending an email.

Instead, it should be possible to use Curl to send an email by directly connecting to Gmail.

The RCurl package should be able to provide this. According to the SSL SMTP example, an email (subject + body) can be uploaded with code (converted from C) such as the following:

library(RCurl) <- function(x) return(x)

gmail.rcurl.send <- function( username, password,, subject, body ) { <- paste(
    paste('Subject:', subject),
    '', body, '', sep="\r\n")
  curl <- getCurlHandle()
  curlSetOpt( .opts=list(
    "useragent"='Mozilla 5.0',
    "use.ssl"=3, # AKA CURLUSESSL_ALL 
  ), curl=curl )
  getURL('smtp://', curl=curl, verbose=TRUE)

Unfortunately, this crashes R -- it appears to be a bug in RCurl, possibly due to a lack of SMTP support ( a call to curlVersion() shows that SMTP and SMTPS are supported, but listCurlOptions() does not include mail.from or mail.rcpt).

Instead, Curl must be called directly via system. Sure, this is ugly, and yes, system should never be used, but it was RCurl that drove us to this. Remember to sanitize your inputs (in this case, the email addresses and the password)!

gmail.curl.send <- function( username, password,, subject, body ) { <- paste(
    paste('Subject:', subject),
    '', body, '', sep="\r\n")
  # IMPORTANT: username, password, and must be cleaned!
  cmd <- paste('curl -n --ssl-reqd --mail-from "<',
               '>" --mail-rcpt "<',
               '<" --url ',
               ' --user "', username, ':', password, 
               '" -T -', sep='')

This works correctly, as one would expect: it's hard to go wrong with a simple shell call to Curl. Note the use of the input parameter in the system call: this creates a temp file with the email contents, which Curl then uploads to Gmail using the -T flag.

No comments:

Post a Comment