How to send email from R with the help of Amazon SES and mailR

Now we have a LEMRS stack. How about adding email? Amazon’s Simple Email Service seems anything but simple when you confront its voluminous documentation, so here’s a tl;dr version for a relatively simple set-up with R. One of the big attractions here is that if you’re already running an Amazon instance, you can send thousands of emails every month at no additional cost. Otherwise, it’s 10 cents per thousand emails. You may find it worthwhile to sign up for Amazon Web Services just for the ability to send email.

You’ll need to install the mailR package. Amazon SES requires authentication (a special SES username / password). The mailR documentation suggests something like this:

Smtp.Username = ""
Smpt.Password = ""
Smtp.Server = ""
Smtp.Port = ""
Smtp.From = ""

send.mail(from = Smtp.From,
          to = "Name <>"),
          replyTo = "Name <>"
          subject = "Subject of the email",
          body = "Body of the email",
          smtp = list( = Smtp.Server,
                      port = Smtp.Port, 
             = Smpt.Username, 
                      passwd = Smpt.Password, 
                      ssl = TRUE),
          authenticate = TRUE,
          send = TRUE)

All you need to use Amazon SES is the right values for those five Smtp variables. Before we get to how to obtain those values, however, let me suggest that once you have them, you put the assignment of those variables in a file you call something like credentials.R and pull them into your app.R file with a command like:

source("credentials.R", local=TRUE)

Then you want to make sure you never add your credentials.R file to GitHub or otherwise accidentally share it.

To get the values for the first four variables, sign into your account at Amazon Web Services and then go here. For the last one you have to use an Amazon-verified email address (ie, they send an email to that address and you click the link in the email to verify the address). You can verify email addresses one at a time here. Or, if you have a domain name and your registrar will let you add two TXT and three CNAME records, you can verify all the email addresses @your-domain.

At this point you can test sending email, but both the to and from addresses have to be Amazon-verified addresses. So if you do one-at-a-time verification, verify several addresses so you can test whether you have everything set up right.

To get out of that sandbox, you have to file an SES Sending Limits Increase Case with Amazon SES. The form is shorter than the instructions, which are here. The instructions say approval can require 24 hours, but in about an hour my account was approved to send 50K emails a month, which I think is far far more than I will ever need.

(I asked for 40 a month. No, not 40K a month, just 40.)

Leave a Reply

Your email address will not be published. Required fields are marked *