When setting up automated reporting workflows, a key component is distributing the report to the various stakeholders so they can review it. One useful technique is to send a “push notification” by email to alert everyone that a report or analysis task has just been completed. Luckily, it isn’t hard to send out email from R by using Microsoft Outlook.
Prerequisites
This technique will only work on Windows computers with Outlook installed, and requires the RDCOMClient package. You can install it with the following command:
install.packages("RDCOMClient")
As of the time of writing, the RDCOMClient package isn’t available in CRAN for all versions of R. If you receive an error such as:
Warning in install.packages :
package ‘RDCOMClient’ is not available (for R version 3.5.1)
You will need to install DCOMClient from the source repository with:
install.packages("RDCOMClient", repos = "http://www.omegahat.net/R")
Sending plain text email
Sending a plain text email to someone from R is easy! All you need to do is:
# Load the DCOM library
library (RDCOMClient)
# Open Outlook
Outlook <- COMCreate("Outlook.Application")
# Create a new message
Email = Outlook$CreateItem(0)
# Set the recipient, subject, and body
Email[["to"]] = "recipient1@test.com; recipient2@test.com; recipient3@test.com"
Email[["cc"]] = ""
Email[["bcc"]] = ""
Email[["subject"]] = "Quarterly Sales Analysis Updated"
Email[["body"]] =
"The quarterly sales analysis has been updated.
You can find it at: D:\\Reports\\Sales Analysis.xlsx"
# Send the message
Email$Send()
# Close Outlook, clear the message
rm(Outlook, Email)
Sending HTML email
If you want to format your message so it looks cleaner and take advantage of improved formatting you can also send HTML messages using the htmlbody instead of the body attribute:
# Load the DCOM library
library (RDCOMClient)
# Open Outlook
Outlook <- COMCreate("Outlook.Application")
# Create a new message
Email = Outlook$CreateItem(0)
# Set the recipient, subject, and body
Email[["to"]] = "recipient1@test.com; recipient2@test.com; recipient3@test.com"
Email[["cc"]] = ""
Email[["bcc"]] = ""
Email[["subject"]] = "Quarterly Sales Analysis Updated"
Email[["htmlbody"]] =
"<h1>Quarterly Sales Analysis</h1>
<p>The quarterly sales analysis has been updated.</p>
<p>You can find it at
<a href='file:\\\D:\\Reports\\Sales Analysis.xlsx'>
D:\\Reports\\Sales Analysis.xlsx
</a>
</p>"
# Send the message
Email$Send()
# Close Outlook, clear the message
rm(Outlook, Email)
Adding attachments
Suppose that you have a data frame or file that you would like to send as an attachment to your message. All you need to do is add the attachments.add attribute to your email object and pass it the path of the file you want to attach.
# Attach a file to the message
Email[["attachments"]]$Add("D:\\Reports\\Sales Analysis.xlsx")
Setting the importance and sensitivity
You can also set the importance and sensitivity of the message using the importance and sensitivity attributes. Note that the importance and sensitivity are expressed as integers:
Importance
- Low
- Normal (default)
- High
Sensitivity
- Normal (default)
- Personal
- Private
- Confidential
# Set the importance
# 0 - Low, 1 - Normal, 2 - High
Email[["importance"]] = "2"
# Set the sensitivity
# 0 - Normal, 1 - Personal, 2 - Private, 3 - Confidential
Email[["sensitivity"]] = "3"
Send messages from an alternate account
If you have permission to send messages on the behalf of someone else in your organization, you can use the sentonbehalfofname attribute to specify which email address you want to message to be sent from.
# Send the message from an alternate account
Email[["sentonbehalfofname"]] = "alternate-sender@test.com"
Request read and delivery receipts
In order to check whether your message was successfully delivered and / or read, you can request read and delivery receipts by setting the readreceiptrequested and originatordeliveryreportrequested attributes to true.
# Request a read receipt
Email[["readreceiptrequested"]] = TRUE
# Request a delivery receipt
Email[["originatordeliveryreportrequested"]] = TRUE
Further ways to take advantage of email from R
There are a number of ways you can integrate email into your R workflows further to increase productivity:
- For the sake of clarity, I used fixed strings to set the recipients in the examples above. You could also use a (dynamically generated) semicolon separated vector of email addresses to specify who should receive the messages.
- Likewise you could also incorporate the results of your analysis into the actual subject line or body by using the paste() function. Receiving a message that says “Quarterly Sales Analysis Updated – 33% YoY Increase” is a lot more likely to get attention!
- You can use R’s markdown capability to compose and send information rich HTML messages quickly and easily.
Want to learn more? Read Advanced email in R: embedding images and markdown to learn how to use Markdown to create messages and add embedded images.
Need help automating your reporting workflows, or want to share the latest trick you learned? Reach out to me by leaving a comment or using the contact form and I would be happy to hear from you!
Leave a Reply