Sending business email: Automate the boring stuff with R

Notes and slides from my presentation at the LondonR meetup

I had the pleasure of speaking at the December 2019 LondonR Meetup organised by the lovely people at Mango Solutions. The topic of my presentation was on automation within the R ecosystem. I talked about various tasks that one is able to automate and my experience using R. However, the real focus of the talk, and area where I could add most value, was how to automate the sending of MS Outlook emails.

In summary, my solution was, using the RDCOMClient package, to use R as a component object model (COM) client to invoke MS Outlook methods and properties. This solution is flexible, requires no administrative rights, and straight-forward to implement. This post will briefly outline my approach. Please note, the building of fancy-pants HTML email content is outside the scope of this post.

Initial set up

n.b. the usefulness of this post depends heavily on the RDCOMClient package developed by The Omega Project for Statistical Computing

# Load the required packages
library(tidyverse)
library(htmlTable)
library(lubridate)
# install.packages("RDCOMClient", repos = "http://www.omegahat.net/R")
library("RDCOMClient")
library(readr)

Initiate the COM application programming interface.

The Microsoft component object model is perhaps best described as a object-oriented standard. That is to say, Microsoft employees have kindly written up and documented the majority of their software's interactivity into a binary standard that we can invoke programmatically. Read more here.

OutApp <- COMCreate("Outlook.Application")

Now that we are up and running with R as a COM client we can invoke MS Outlook methods ‘$’ and access properties ‘[[’. Our first step is to create an email:

## create an email
outMail <-  OutApp$CreateItem(0)

From here we can start fleshing out the content, adding in recipients, and attaching anything we want:

## configure  email parameter
outMail[["To"]] <-  "client@somecompany.com"
outMail[["subject"]] <- "Hello"
outMail[["body"]] <- "Is it me you're looking for?"
outMail[["Attachments"]]$Add("C:/Users/somepath/Admin.html")


## send it
outMail$Send()

That's it.

You have successfully sent an automated email. I told you it was straight forward.

The good news is that this is just the start - from here you could append your professional HTML email signature, play around with formatting, and even send HTML RMarkdown reports directly in the mail body.

You can find my slides here.

Feel free to get in contact with me if you have any questions or comments.