R Lang is a powerful programming language for mathematical modeling and statistical analysis. But did you know R can also be used to send emails? This is a useful feature and allows programmers and scientists to sent alerts and dataset results via email from with r scripts and RStudio. In this post we'll cover two popular methods for sending email in R.
Setup your environment
Install RLang by downloading an official distribution or by using a package manager. On OSX you can use Brew to install R like so:
Next we can verify r is installed and functioning by running the RScript command from a terminal or command line. Verify RScript installation with the version argument
RLang SMTP libraries
In order to send email in r we need to install some packages. The main packages we will cover in this post are:
Each library has pros and cons and we will go over each to help you decide which package to use when sending an email.
To install packages in R lang we use the
function. Run the RScript commands below to install all the packages we need:
This will make our desired packages available in our R code when calling the
Send email with sendmailR
package is a popular library for sending email to an SMTP server. It has good support for most formats but does not accept authentication. We can still use
to send emails to SMTP servers but the emails will be delivered from our own machine via SMTP and not through an MTA mail transfer agent. This is fine for many use cases.
Easy setup for automated email sending with no user input.
Does not support authentication. For sending with authentication use the
package covered in the next section.
Create a new R script and import the
for http requests.
Then send using the
Send with Blastula
Another great package for emailing in Rlang is
. In contrast to
it supports SMTP authentication too meaning we can send emails through an SMTP server that we control.
Support MTA mail transfer with authentication (username and password).
Requires a credentials file or user input to enter password. For this reason it may not be suitable for automated scripting.
First import blastula:
Then compose and send an email:
Testing email sending
If you wish to send emails in R and test the results try MailSlurp. You can create unlimited test email accounts and trap outbound email messages before they are sent. Create a free account today to get started.