Saturday, September 10, 2022

sendmailR: A Simple SMTP Email Client Package for R

It's been a while, but this R package was just too cool not to share!

I'm going to introduce you to a really useful R package called sendmailR.  sendmailR is almost too easy to use, but I think that's what I love most about it.  The package is an SMTP email client which just needs your SMTP server information to get you up and running.  You may be asking yourself, "RDataguy we already covered sending email in a post using the RDCOMClient package, why do I need this package?"  Well, I'll get into that next!

Why use 'sendmailR'?

Sometimes the device or server you're working on does not have access to Microsoft Outlook and so you're not able to utilize COM Objects to create and send an email.  sendmailR only needs your device to be able to communicate with your SMTP email server and you're good to start sending emails.  Also, as you'll see below, the developer did a fantastic job providing you with simple functions to get you emailing as quickly as possible.

Example

To start, you're going to need two things other than your normal email requirements (e.g. To, From, Subject, Body, Attachments, etc.).  You're going to need the SMTP Server name and SMTP Server Port number (default is Port 25).  Once you have that information, here's the code below.

# Load Packages
library(sendmailR)

# Compose email
from <- "your_email@your_domain.com"
to <- "recipient_email@their_domain.com"
subject <- "Hello from R"
# Attachments attach1 <- "myfile1.pdf"
attach2 <- "myfile2.xlsx"
body <- list("I'm so happy this is working!",attach1, attach2)
# Send email sendmail(from, to, subject, body, control=list(smtpServer="mail.<your domain>.com"))s

Note: You can add the 'smtpPort' argument to the control list argument in the sendmail function if your port number is something other than 25.

Conclusion

As you can see, using sendmailR to send your customers emails from R is extremely simple to setup and use.  Good luck and have fun coding!

RDataGuy