How to Send an Email in R

On November 30, 2019
15min read
Artur Hebda Full Stack Developer @Railsware

R is a powerful solution to deal with statistics-heavy projects and explore datasets. It can be used for UX monitoring, data-based predictions, and much more. Some say the R programming language can do everything and we’re not going to disprove such a bold claim. What we’re interested in is how you can share the results of your R-based analysis with different stakeholders. Of all possible channels for this, email is the most common one. So, let’s explore multiple ways of how to send an email with R.

Packages for sending emails from R

Here are the R packages you can use for sending emails:

PackageDescriptionLatest versionDependency
sendmailRA portable solution for sending emails from R (contains
a simple SMTP client).
1.2-1R 3.0.0+
mailAn easy to use package for sending emails from R.1.0R 2.0.0+
mailRA wrapper around Apache Commons Email for sending
emails from R.
0.6N/A
blastulaA package for creating and sending HTML emails from R
through an SMTP server or Mailgun API.
0.2.1
R 3.2.1+
blatrA wrapper around Blat – a Windows command line utility
that sends emails
via SMTP or posts to Usenet via NNTP.
1.0.1N/A
gmailRA package for sending emails via the Gmail’s RESTful API.1.0.0R 3.0.0+
IMmailgunA package for sending emails via the Mailgun API.0.1.2N/A
emayiliA package for sending emails from R via an SMTP server.0.1.1N/A
RDCOMClientA Windows-specific package for sending emails in R from
the Outlook app. 
0.94-0N/A
ponyexpressA package to automate email sending from R via Gmail
(based on the gmailR package).
N/AN/A

We won’t focus on all of them, but we will introduce the most common and convenient options.

Sending emails in R via SMTP

Whichever R package you choose, keep in mind that you need an SMTP server to send emails. In our examples, we’ll use Mailtrap Email Sandbox, a safe environment that captures SMTP traffic and helps you avoid spamming recipients with test emails. Besides catching testing emails from staging, using the Sandbox, you can analyze your email’s content for spam with the help of an already integrated Apache SpamAssassin filter and validate HTML/CSS. For more on Email Sandbox functionality, use cases, and benefits, read the Mailtrap Getting Started Guide.

sendmailR 

sendmailR can be used for sending all sorts of email notifications such as completed jobs and scheduled tasks. At the same time, you can distribute analytical results to stakeholders using this R package as well. sendmailR is mostly used for SMTP servers without authentication. That’s why we won’t use Email Sandbox in the following examples. Let’s install the package first:

install.packages("sendmailR",repos="http://cran.r-project.org")

Next, we create a data structure called Server, which is a map with a single key value pair – key: smtpServer, value: smtp.example.io:

Server<-list(smtpServer= "smtp.example.io")

Now, let’s write a few R lines to send a simple email:

library(sendmailR)
from <- sprintf("<user@sender.com>","The Sender") # the sender’s name is an optional value
to <- sprintf("<user@recipient.com>")
subject <- "Test email subject"
body <- "Test email body"
sendmail(from,to,subject,body,control=list(smtpServer= "smtp.example.io"))

The following code sample is for sending an email to multiple recipients:

from <- sprintf("<user@sender.com>","The Sender")
to <-sprintf(c("<user@recipient.com>","<user2@recipient.com>", "<user3@recipient.com>")
subject <- "Test email subject"
body <- "Test email body"
sapply(to,function(x) sendmail(from,to=x,subject,body,control=list(smtpServer= "smtp.example.io"))

And now, let’s send an email with an attachment as well:

from <- sprintf("<user@sender.com>","The Sender")
to <- sprintf("<user@recipient.com>")
subject <- "Test email subject"
body <- "Test email body"
attachmentPath <-"C:/.../Attachment.png"
attachmentObject <-mime_part(x=attachmentPath,name=attachmentName)
bodyWithAttachment <- list(body,attachmentObject)
sendmail(from,to,subject,bodyWithAttachment,control=list(smtpServer= "smtp.example.io"))

NB: To send emails with sendmailR, you may also need to configure your machine so it can send emails from your local host. We’ve covered this step in How To Set Up An SMTP Server.

mailR

If you employ an authentication-based SMTP server, you’d better pick the mailR package. It’s a wrapper around Apache Commons Email, an email library built on top of the Java Mail API. Due to this, mailR has a dependency on the rJava package, a low-level interface to Java VM. This requires Java Runtime Environment to be installed. You can download it from Oracle. In case of problems with pointing to the right Java binary, refer to this troubleshooting guide on GitHub. In practice, this may cause a bit of trouble when deploying in some environments. Nevertheless, mailR is a rather popular solution to automate sending emails with the R that offers the following:

  • multiple recipients (Cc, Bcc, and ReplyTo)
  • multiple attachments (both from the file system and URLs)
  • HTML formatted emails 

Install the package:

install.packages("mailR",repos="http://cran.r-project.org")

Now, we can use the Mailtrap Email Sandbox SMTP server that requires authentication to send an email:

library(mailR)
send.mail(from = "user@sender.com",
          to = "user@recipient.com",
          subject = "Test email subject",
          body = "Test emails body",
          smtp = list(host.name = "smtp.mailtrap.io", port = 25,
                      user.name = "********",
                      passwd = "******", ssl = TRUE),
          authenticate = TRUE,
          send = TRUE)

Insert your Mailtrap credentials (user.name and passwd) and pick any SMTP port of 25, 465, 587, 2525.

Here is how to send an email to multiple recipients:

library(mailR)
send.mail(from = "user@sender.com",
          to = c("Recipient 1 <user1@recipient.com>", "Recipient 2 <user@recipient.com>"),
          cc = c("CC Recipient <cc.user@recipient.com>"),
          bcc = c("BCC Recipient <bcc.user@recipient.com>"),
          replyTo = c("Reply to Recipient <reply-to@recipient.com>"),
          subject = "Test email subject",
          body = "Test emails body",
          smtp = list(host.name = "smtp.mailtrap.io", port = 25,
                      user.name = "********",
                      passwd = "******", ssl = TRUE),
          authenticate = TRUE,
          send = TRUE)

Now, let’s add a few attachments to the email:

library(mailR)
send.mail(from = "user@sender.com",
          to = c("Recipient 1 <user1@recipient.com>", "Recipient 2 <user@recipient.com>"),
          cc = c("CC Recipient <cc.user@recipient.com>"),
          bcc = c("BCC Recipient <bcc.user@recipient.com>"),
          replyTo = c("Reply to Recipient <reply-to@recipient.com>"),
          subject = "Test email subject",
          body = "Test emails body",
          smtp = list(host.name = "smtp.mailtrap.io", port = 25,
                      user.name = "********",
                      passwd = "******", ssl = TRUE),
          authenticate = TRUE,
          send = TRUE,
          attach.files = c("./attachment.png", "https://dl.dropboxusercontent.com/u/123456/Attachment.pdf"),
          file.names = c("Attachment.png", "Attachment.pdf"), #this is an optional parameter
          file.descriptions = c("Description for Attachment.png", "Description for Attachment.pdf")) #this is an optional parameter

Eventually, let’s send an HTML email from R:

library(mailR)
send.mail(from = "user@sender.com",
          to = "user@recipient.com",
          subject = "Test email subject",
          body = "<html>Test <strong>email</strong> body</html>",
          smtp = list(host.name = "smtp.mailtrap.io", port = 25,
                      user.name = "********",
                      passwd = "******", ssl = TRUE),
          authenticate = TRUE,
          send = TRUE)

You can also point to an HTML template by specifying its location, as follows:

body = "./Template.html",

blastula

The blastula package allows you to craft and send responsive HTML emails in R programming. We’ll review how to send emails via the SMTP server, however, blastula also supports the Mailgun API. 

Install the package:

install.packages("blastula",repos="http://cran.r-project.org")

and load it:

library(blastula)

Compose an email using Markdown formatting. You can also employ the following string objects:

  • add_readable_time – creates a nicely formatted date/time string for the current time 
  • add_image – transforms an image to an HTML string object

For example,

date_time <- add_readable_time() # => "Thursday, November 28, 2019 at 4:34 PM (CET)"
img_file_path <- "./attachment.png" # => "<img cid=\"mtwhxvdnojpr__attachment.png\" src=\"data:image/png;base64,iVBORw0KG...g==\" width=\"520\" alt=\"\"/>\n"
img_string <- add_image(file = img_file_path)

When composing an email, you will need the c() function to combine the strings in the email body and footer. 

You can use three main arguments: body, header, and footer. If you have Markdown and HTML fragments in the email body, use the md() function. Here is what we’ve got:

library(blastula)
email <-
compose_email(
  body = md(
    c("<html>Test <strong>email</strong> body</html>",
img_string
      )
    ),
 footer = md(
    c(
      "Test email footer", date_time, "."
      )
  )
)

Preview the email using attach_connect_email(email = email)

Now, let’s send the email. This can be done with the smtp_send() function through one of the following ways:

  1. Providing the SMTP credentials directly via the creds() helper: 
smtp_send(
  email = email,
  from = "user@sender.com",
  to = "user@recipient.com",
  credentials = creds(
    host = "smtp.mailtrap.io",
    port = 25,
    user = "********"
  )
)
  1. Using a credentials key that you can generate with the create_smtp_creds_key() function: 
create_smtp_creds_key(
  id = "mailtrap",
  host = "smtp.mailtrap.io",
  port = 25,
  user = "********"
)
smtp_send(
  email = email,
  from = "user@sender.com",
  to = "user@recipient.com",
  credentials = creds_key("mailtrap")
)
  1. Using a credentials file that you can generate with the create_smtp_creds_file() function:
create_smtp_creds_file(
  file = "mailtrap_file",
  host = "smtp.mailtrap.io",
  port = 25,
  user = "********"
)
smtp_send(
  email = email,
  from = "user@sender.com",
  to = "user@recipient.com",
  credentials = creds_file("mailtrap_file")
)

NB: There is no way to programmatically specify a password for authentication. The user will be prompted to provide one during code execution.

emayili 

emayili is the last package on our list for sending emails in R via SMTP. The package works with all SMTP servers and has minimal dependencies. Install it from GitHub and let’s move on:

install.packages("remotes")
library(remotes)
remotes::install_github("datawookie/emayili")

Emayili has two classes at the core:

  • envelope – to create emails 
  • server – to communicate with the SMTP server

Let’s create an email first:

library(emayili)
email <- envelope() %>%
  from("user@sender.com") %>%
  to("user@recipient.com") %>%
  subject("Test email subject") %>%
  body("Test email body")

Now, configure the SMTP server:

smtp <- server(host = "smtp.mailtrap.io",
               port = 25,
               username = "********",
               password = "*********")

To send the email to multiple recipients, enhance your emails with Cc, Bcc, and Reply-To header fields as follows:

email <- envelope() %>%
  from("user@sender.com") %>%
  to(c("Recipient 1 <user1@recipient.com>", "Recipient 2 <user@recipient.com>")) %>%
  cc("cc@recipient.com") %>%
  bcc("bcc@recipient.com") %>%
  reply("reply-to@recipient.com") %>%
  subject("Test email subject") %>%
  body("Test email body")

You can also use the attachment() method to add attachments to your email:

email <- email %>% attachment(c("./attachment.png", "https://dl.dropboxusercontent.com/u/123456/Attachment.pdf"))

Eventually, you can send your email with:

smtp(email, verbose = TRUE)

Sending emails via Gmail API – gmailR

Today, Gmail is one of the most popular email services. It provides RESTful API for a bunch of functionalities, such as:

  • send/receive HTML emails with attachments
  • CRUD (create, read, update, and delete) operations with messages, drafts, threads, and labels 
  • access control of your Gmail inbox
  • and so on

For sending emails from R via Gmail API, you need two things: the gmailR package and the API access. Let’s start with the latest, which requires four steps to be done:

  1. Create a project in the Google API Console
  2. Enable Gmail API
  3. Set up credentials and authentication with OAuth 2.0
  4. Download a JSON file with your credentials

We’ve described all these steps in How to send emails with Gmail API, so feel free to reference this blog post. After you’ve accomplished the preparation stage, get back to gmailR. The package is available on CRAN, so you can install, as follows:

install.packages("gmailr", repos="http://cran.r-project.org")

and load in your R script:

library(gmailr)

Now, you can use your downloaded JSON credentials file. Employ the use_secret_file() function. For example, if your JSON file is named GmailCredentials.json, this will look, as follows:

use_secret_file("GmailCredentials.json")

After that, create a MIME email object:

email <- gm_mime() %>%
  gm_to("user@recipient.com") %>%
  gm_from("user@sender.com") %>%
  gm_subject("Test email subject") %>%
  gm_text_body("Test email body")

To create an HTML email, use markup to shape your HTML string, for example:

email <- gm_mime() %>%
  gm_to("user@recipient.com") %>%
  gm_from("user@sender.com") %>%
  gm_subject("Test email subject") %>%
  gm_html_body("<html>Test <strong>email</strong> body</html>")

To add an attachment, you can:

  • use the gm_attach_file() function, if the attachment has not been loaded into R. You can specify the MIME type yourself using the type parameter or let it be automatically guessed by mime::guess_type
email <- gm_mime() %>%
  gm_to("user@recipient.com") %>%
  gm_from("user@sender.com") %>%
  gm_subject("Test email subject") %>%
  gm_html_body("<html>Test <strong>email</strong> body</html>") %>%
  gm_attach_file("Attachment.png")
  • use attach_part() to attach the binary data to your file:
email <- gm_mime() %>%
  gm_to("user@recipient.com") %>%
  gm_from("user@sender.com") %>%
  gm_subject("Test email subject") %>%
  gm_html_body("<html>Test <strong>email</strong> body</html>") %>%
  gm_attach_part(part = charToRaw("attach me!"), name = "please")

If you need to include an image into HTML, you can use the <img src=”cid:xy”> tag to reference the image. First create a plot to send, and save it to AttachImage.png:

# 1. use built-in mtcars data set
my_data <- mtcars
# 2. Open file for writing
png("AttachImage.png", width = 350, height = 350)
# 3. Create the plot
plot(x = my_data$wt, y = my_data$mpg,
  pch = 16, frame = FALSE,
  xlab = "wt", ylab = "mpg", col = "#2E9FDF")
# 4. Close the file
dev.off()

Now, create an HTML email that references the plot as foobar:

email <- gm_mime() %>%
  gm_to("user@recipient.com") %>%
  gm_from("user@sender.com") %>%
  gm_subject("Test email subject") %>%
  gm_html_body(
    '<html>Test <strong>email</strong> body</html>
    <br><img src="cid:foobar">'
  ) %>%
  gm_attach_file("AttachImage.png", id = "foobar")

Eventually, you can send your email:

gm_send_message(email)

Sending emails from Outlook – RDCOMClient

R has a package for sending emails from Microsoft Outlook as well. It’s called RDCOMClient and allows you to connect to DCOM architecture, which you can consider an API for communicating with Microsoft Office in Windows environments. Let’s explore how to connect R to the Outlook app installed on your Windows.

Install RDCOMClient via an option of your choice:

  • from CRAN:
install.packages("RDCOMClient")
  • via devtools:
devtools::install_github("omegahat/RDCOMClient")
  • from the Windows command line:
R CMD INSTALL RDCOMClient

Warning: if you receive a message like package ‘RDCOMClient’ is not available (for R version 3.5.1)” during the installation from CRAN, try to install RDCOMClient from the source repository:

install.packages("RDCOMClient", repos = "http://www.omegahat.net/R")

Load the package, open Outlook, and create a simple email:

library(RDCOMClient)
Outlook <- COMCreate("Outlook.Application")
Email = Outlook$CreateItem(0)
Email[["to"]] = "user@recipient.com"
Email[["subject"]] = "Test email subject"
Email[["body"]] = "Test email body"

If you need to change the default From: field and send from a secondary mailbox, use:

Email[["SentOnBehalfOfName"]] = "user@sender.com"

Here is how you can specify multiple recipients, as well as Cc and Bcc headers:

Email[["to"]] = "user1@recipient.com, user2@recipient.com"
Email[["cc"]] = "cc.user@recipient.com"
Email[["bcc"]] = "bcc.user@recipient.com"

To create an HTML email, use [["htmlbody"]]. You can simply add your HTML in the R code as follows:

library(RDCOMClient)
Outlook <- COMCreate("Outlook.Application")
Email = Outlook$CreateItem(0)
Email[["to"]] = "user@recipietn.com"
Email[["subject"]] = "Test email subject"
Email[["htmlbody"]] =
"<html>Test <strong>email</strong> body</html>"

Let’s also add an attachment: 

library(RDCOMClient)
Outlook <- COMCreate("Outlook.Application")
Email = Outlook$CreateItem(0)
Email[["to"]] = "user@recipient.com"
Email[["subject"]] = "Test email subject"
Email[["htmlbody"]] =
"<html>Test <strong>email</strong> body</html>"
Email[["attachments"]]$Add("C:/.../Attachment.png")

Now, you can send the email:

outMail$Send()

Sending emails with Mailtrap Email API 

Besides email testing, Mailtrap offers Email API/SMTP Service, which is used for sending and lets you keep all of your email infrastructures in one place. Using Email API, you can get timely reports on your deliverability performance, analyze it, and gain better control over it. 

With Email API, you get access to 60 days worth of email history and the ability to inspect individual emails or by their category. For example, you can see unique opens and open rates, bounce rates, and other important metrics that help you understand how your “welcome” or any other email type performs. 

How to send bulk emails from R?

Let’s say your mail list includes many more than ten recipients and you need to send bulk emails from R. We’ll show you how this can be done via Web API (gmailR) and SMTP (mailR).

Bulk emails with gmailR

As an example, we’ll inform recipients of how much they won in the lottery. For this, we need:

  • an enabled API access on your Google account. 
  • an installed gmailr R package.
  • a set of R packages for data iteration: readr, dplyr, and purrr (or plyr as an alternative).
  • a file containing the variable bits (lottery wins), Variables.csv, with the following format:
lastname,firstname,win_amount,email_address
SMITH,JOHN,1234,johnsmith@winner.com
LOCKWOOD,JANE,1234,janelockwood24@example.com

Now, let’s go through the mail steps to create an R script for bulk emails.

  • Load the packages and files we need:
suppressPackageStartupMessages(library(gmailr))
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(plyr))
suppressPackageStartupMessages(library(purrr))
library(readr) # => if you don’t have it, run: install.packages("readr", repos="http://cran.r-project.org")
my_dat <- read_csv("Variables.csv") 
  • Create a data frame that will insert variables from the file into the email: 
this_hw <- "Lottery Winners"
email_sender <- 'Best Lottery Ever <info@best-lottery-ever.com>'
optional_bcc <- 'Anonymous <bcc@example.com>'
body <- "Hi, %s.
Your lottery win is %s.
Thanks for betting with us!
"
edat <- my_dat %>%
    mutate(
        To = sprintf('%s <%s>', firstname, email_address),
        Bcc = optional_bcc,
        From = email_sender,
        Subject = sprintf('Lottery win for %s', win_amount),
        body = sprintf(body, firstname, win_amount)) %>%
    select(To, Bcc, From, Subject, body)
write_csv(edat, "data-frame.csv")

The data frame will be saved to data-frame.csv. This will provide an easy-to-read record of the composed emails. 

Now, convert each row of the data frame into a MIME object using the gmailr::mime() function. After that, purrr::pmap() generates the list of MIME objects, one per row of the input data frame:

emails <- edat %>%
  pmap(mime)
str(emails, max.level = 2, list.len = 2)

If you use plyr (install.packages("plyr")), you can do this, as follows:

emails <- plyr::dlply(edat, ~ To, function(x) mime(
  To = x$To,
  Bcc = x$Bcc,
  From = x$From,
  Subject = x$Subject,
  body = x$body))

Specify your JSON credentials file:

use_secret_file("GmailCredentials.json")

And send emails with purrr::safely(). This will protect your bulk emails from failures in the middle:

safe_send_message <- safely(send_message)
sent_mail <- emails %>%
  map(safe_send_message)
saveRDS(sent_mail,
        paste(gsub("\\s+", "_", this_hw), "sent-emails.rds", sep = "_"))

List recipients with TRUE in case of errors:

errors <- sent_mail %>%
  transpose() %>%
  .$error %>%
  map_lgl(Negate(is.null))

Take a look at the full code now:

suppressPackageStartupMessages(library(gmailr))
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(plyr))
suppressPackageStartupMessages(library(purrr))
library(readr) # => if you don’t have it, run: install.packages("readr", repos="http://cran.r-project.org")
my_dat <- read_csv("Variables.csv")
this_hw <- "Lottery Winners"
email_sender <- 'Best Lottery Ever <info@best-lottery-ever.com>'
optional_bcc <- 'Anonymous <bcc@example.com>'
body <- "Hi, %s.
Your lottery win is %s.
Thanks for betting with us!
"
edat <- my_dat %>%
    mutate(
        To = sprintf('%s <%s>', firstname, email_address),
        Bcc = optional_bcc,
        From = email_sender,
        Subject = sprintf('Lottery win for %s', win_amount),
        body = sprintf(body, firstname, win_amount)) %>%
    select(To, Bcc, From, Subject, body)
write_csv(edat, "data-frame.csv")
emails <- edat %>%
  pmap(mime)
str(emails, max.level = 2, list.len = 2)
use_secret_file("GmailCredentials.json")
safe_send_message <- safely(send_message)
sent_mail <- emails %>%
  map(safe_send_message)
saveRDS(sent_mail,
        paste(gsub("\\s+", "_", this_hw), "sent-emails.rds", sep = "_"))
errors <- sent_mail %>%
  transpose() %>%
  .$error %>%
  map_lgl(Negate(is.null))

Bulk emails with mailR

If you want to send bulk emails with SMTP, make sure to have an appropriate SMTP server and install the mailR package. Once again, we’ll need a .csv file that will contain the data frame you want to integrate into the email. The data should be separated by a special character such as a comma, a semicolon, or a tab9. For example:

lastname; firstname; win_amount; email_address
SMITH; JOHN; 1234; johnsmith@winner.com
LOCKWOOD; JANE; 1234; janelockwood24@example.com

What you need to do next:

  • Build the HTML email body for a given recipient using the message_text function:
message_text <- function(x) sprintf('Hello %s %s!\nCongratulation to your win.\nYour prize is XXX.\nBet with the Best Lottery Ever!', x$firstname, x$lastname)

Load the package and read in the mail list:

library(mailR)
mail_list <- read.csv2("Variables.csv",as.is=TRUE)

Values in the Variables.csv should be separated with a semicolon (;). You can configure settings to read the data frame using the read.table or read.csv functions.

Create a file to write the information of each individual row in the mail_list after each email is sent.

my_file <- file("mail.out",open="w")
# … write data here
close(my_file)

Perform the batch emailing to all students in the mail list:

for (recipient in 1:nrow(mail_list)) {
  body <- message_text(mail_list[recipient,])
  send.mail(from="info@best-lottery-ever.com",
    to=as.character(mail_list[recipient,]$email_address),
    subject="Lottery Winners",
    body=body,
    html=TRUE,
    authenticate=TRUE,
    smtp = list(host.name = "smtp.mailtrap.io",
    user.name = "*****", passwd = "*****", ssl = TRUE),
    encoding = "utf-8",send=TRUE)
  print(mail_list[recipient,])
  Sys.sleep(runif(n=1,min=3,max=6))
  #write each recipient to a file
  result_file <- file("mail.out",open="a")
  writeLines(text=paste0("[",recipient,"] ",
    paste0(as.character(mail_list[recipient,]),collapse="\t")),
    sep="\n",con=result_file)
  close(result_file)
}

And here is the full code:

message_text <- function(x) sprintf('Hello %s %s!\nCongratulation to your win.\nYour prize is XXX.\nBet with the Best Lottery Ever!', x$firstname, x$lastname)
library(mailR)
mail_list <- read.csv2("Variables.csv",as.is=TRUE)
my_file <- file("mail.out",open="w")
# … write data here
close(my_file)
for (recipient in 1:nrow(mail_list)) {
  body <- message_text(mail_list[recipient,])
  send.mail(from="info@best-lottery-ever.com",
    to=as.character(mail_list[recipient,]$email_address),
    subject="Lottery Winners",
    body=body,
    html=TRUE,
    authenticate=TRUE,
    smtp = list(host.name = "smtp.mailtrap.io",
    user.name = "*****", passwd = "*****", ssl = TRUE),
    encoding = "utf-8",send=TRUE)
  print(mail_list[recipient,])
  Sys.sleep(runif(n=1,min=3,max=6))
  #write each recipient to a file
  result_file <- file("mail.out",open="a")
  writeLines(text=paste0("[",recipient,"] ",
    paste0(as.character(mail_list[recipient,]),collapse="\t")),
    sep="\n",con=result_file)
  close(result_file)
}

How to test email sending in R with Mailtrap

If you choose to send emails from R via SMTP, then Mailtrap is what you need for testing. It’s a universal service with a fake SMTP server underneath. This means, your test emails are not actually being sent. They go from your app or any other mail client to the SMTP server and are trapped there. Thus, you protect your real recipients from an undesirable experience – they won’t receive any of your test emails. All the aforementioned examples with Mailtrap credentials work in this way. If you need to test anything else, just replace your SMTP credentials with those of Mailtrap and that’s it. For this, you need to sign up first using your email, GitHub or Google account. A FREE FOREVER plan is available! For more on the features and functions provided by Mailtrap, read the Getting Started Guide.

To wrap up

We’ve listed a number of options for sending emails in R, so choose the one that best fits your requirements. For example, if you need to send hundreds (or even thousands) of emails daily, gmailR may be the best solution. On the other hand, sending via SMTP is a more common and reliable way and R provides a few packages for this. So, good luck with your choice!

Article by Artur Hebda Full Stack Developer @Railsware

Comments

1 replies

Buda

Thank you for the article!
Minor mistake in RDCOMClient part : “Email$Send()” instead of “outMail$Send()”.

Comments are closed.