Have you ever wanted to get a text message or an email when your script finished? I have many times! Well, look no farther because if you use Microsoft Outlook desktop application, here is a solution that will work perfectly! This solution uses an R Package called RDCOMClient maintained by http://www.omegahat.net. RDCOMClient is a very useful package because it can be used to interact with COM Objects associated with Microsoft Office applications such as Outlook, Word, Excel, Access, and Power Point. If you've written any Visual Basic scripts, you've likely used COM Objects at some point. Basically, this means you can programatically interact with these applications from R, opening up a whole list of possiblities for you! It's a very useful package and I use it daily!
STEP #1 - The first thing you'll need to do is download the lastest Java SE 8 Runtime Environment (JRE) for your Operating System (OS). You might be asking "Why Java SE 8?" Well, the simple answer is because that's what I know has worked for myself and my friends. Make sure if you're running 64-bit R that you download the 64-bit version of the JRE. You can find the JRE for your OS here.
STEP #2 - After you've successfully installed the JRE for your OS, the next thing you'll need to do is install the rJava library package. I've found the easiest way to do this is right from your console
install.packages("rJava") library(rJava)
Honestly, rJava can be a bit of a hassle to get setup, but if you run into issues, go checkout stackoverflow.com. I used this site to help troubleshoot my issues with my installation.
STEP #3 - Once you have the JRE and a working rJava library, the next thing you need to do is get a copy of the the RDCOM Client library package. You can get it from one of two places. Either from the CRAN or you can navigate here and download the package version that matches your R version. Personally, I prefer downloading from the Omegahat site and then install it from the zip file. The name of the file will be "RDCOMClient_0.93-0.zip" for R 3.5 or RDCOMClient_0.94-0.zip for R 4.0. For a version that will work with an older version of R, navigate to the "old" directory.
Assuming you download the file from Omegahat, installation can be done by using the "Tools" menu "Install Packages..."
Then you'll want to browse to the location where you saved the RDCOMclient zip-file, select it, and click "Install".
STEP #4 - Once RDCOMClient has finished installing, you can run this code! Use an email address or use a phone number followed by carrier text message domains for sending text message(ATT is @mms.att.net, Sprint is @messaging.sprintpcs.com, and Verizon is @vtext.com, etc.)
# HEADER -------------------------------------------- # # Author: Your Name # Copyright (c) Your Name, 2019 # Email: your_email@your_domain.com # # Date: Today's Date # # Script Name: Email / Text Message with Microsoft Outlook # # Script Description: Create and send an email using Microsoft Outlook and RDCOMClient # # # Notes: # RDCOMClient is used to interact with COM Objects associated with Microsoft Office applications # such as Outlook, Word, Excel, Access, and Powerpoint. # # Load Libraries library(rJava) library(RDCOMClient) # Script to send text/email message # access the Outlook application OutApp <- COMCreate("Outlook.Application") # create an email outMail = OutApp$CreateItem(0) # configure email parameter #carrier suffixes for sending to TEXT @mms.att.net, OR @messaging.sprintpcs.com, OR # @vtext.com outMail[["To"]] = "your_email@someplace.com" outMail[["subject"]] = "Testing! - An Email Message from R" outMail[["body"]] = "Hey, it worked!!" # unremark the next line to add attachment # path_to_file = "some_file.pdf" # outMail[["Attachments"]]$Add(path_to_file) # unremark to change "from" (send from secondary mailbox) # outMail[["SentOnBehalfOfName"]] = "your_secondary@someplace.com" # send email or text cat("Email message sent.\n\n") outMail$Send()
RDCOMClient can do a whole lot more than just send emails, I encourage you to check it out and learn more about it!
Enjoy!
Have fun coding!
RdataGuy
Download the code for this lesson below!