Mailr

A utility to send emails from the R programming environment

View the Project on GitHub rpremraj/mailR

Overview

mailR allows users to send emails from R via an accessible SMTP server.

It is developed as a wrapper around Apache Commons Email and offers several desirable features to send mails from R:

What's new

12th May 2014

Features

Bug fixes

20th April 2014

Features

Bug fixes

Sample use cases

  1. Create a workflow to generate business/status reports and have them delivered to you by email using an authorized SMTP server (a critical requirement when dealing with sensitive data).

  2. Periodically send updates on the status of long-running R processes. This is especially helpful when process running on remote machines encounter an error.

  3. Keep your entire team on the same page on the progress of the R process by being able to deliver mails to multiple recipients.

Installation instructions

You can install the latest development version of mailR using devtools:

install.packages("devtools", dep = T)
library(devtools)
install_github("mailR", "rpremraj")

library(mailR)

The latest release of mailR is available on CRAN:

install.packages("mailR", dep = T)

library(mailR)

Usage

To send an email via a SMTP server that does not require authentication:

send.mail(from = "sender@gmail.com",
          to = c("Recipient 1 <recipient1@gmail.com>", "recipient2@gmail.com"),
          cc = c("CC Recipient <cc.recipient@gmail.com>"),
          bcc = c("BCC Recipient <bcc.recipient@gmail.com>"),
          subject="Subject of the email",
          body = "Body of the email",
          smtp = list(host.name = "aspmx.l.google.com", port = 25),
          authenticate = FALSE,
          send = TRUE)

Note that the SMTP server aspmx.l.google.com only works for gmail recipients. Do check your gmail spam folder in case you are testing using this SMTP server.

To send an email via a SMTP server that requires authentication:

send.mail(from = "sender@gmail.com",
          to = c("recipient1@gmail.com", "recipient2@gmail.com"),
          subject = "Subject of the email",
          body = "Body of the email",
          smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "gmail_username", passwd = "password", ssl = TRUE),
          authenticate = TRUE,
          send = TRUE)

To send an email with utf-8 or other encoding:

email <- send.mail(from = "Sender Name <sender@gmail.com>",
                   to = "recipient@gmail.com",
                   subject = "A quote from Gandhi",
                   body = "In Hindi :  थोडा सा अभ्यास बहुत सारे उपदेशों से बेहतर है।
                   English translation: An ounce of practice is worth more than tons of preaching.",
                   encoding = "utf-8",
                   smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "gmail_username", passwd = "password", ssl = T),
               authenticate = TRUE,
                   send = TRUE)

To send an email with one or more file attachments:

send.mail(from = "sender@gmail.com",
          to = c("recipient1@gmail.com", "recipient2@gmail.com"),
          subject = "Subject of the email",
          body = "Body of the email",
          smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "gmail_username", passwd = "password", ssl = TRUE),
          authenticate = TRUE,
          send = TRUE,
          attach.files = c("./download.log", "upload.log"),
          file.names = c("Download log", "Upload log"), # optional parameter
          file.descriptions = c("Description for download log", "Description for upload log"))

To send a HTML formatted email:

send.mail(from = "sender@gmail.com",
          to = c("recipient1@gmail.com", "recipient2@gmail.com"),
          subject = "Subject of the email",
          body = "<html>The apache logo - <img src=\"http://www.apache.org/images/asf_logo_wide.gif\"></html>", # can also point to local file (see next example)
          html = TRUE,
          smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "gmail_username", passwd = "password", ssl = TRUE),
          authenticate = TRUE,
          send = TRUE)

To send a HTML formatted email with embedded inline images:

send.mail(from = "sender@gmail.com",
          to = c("recipient1@gmail.com", "recipient2@gmail.com"),
          subject = "Subject of the email",
          body = "path.to.local.html.file",
          html = TRUE,
          inline = TRUE,
          smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "gmail_username", passwd = "password", ssl = TRUE),
          authenticate = TRUE,
          send = TRUE)

send.mail expects the images in the HTML file to be referenced relative to the current working directory (something to improve upon in the future).

MS Exchange server

I do not have access to an exchange server, so I cannot test mailR against it. Would be great to get a short note if you could successfully use mailR via MS Exchange. Thanks!

Issues/Contibutions

Happy to hear about any issues you encounter using mailR. Simply file a ticket on github or email me. Pathes welcome ;-)