RObservations #2: Mail Merging (Email Blasting) with R

[This article was first published on r – bensstats, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Note: Thank you to Andrew Collier for looking this blog post over!!

Alot of individuals think that to use R or a programming language at work you need to be a software engineer, data analyst, data scientist etc. This can be discouraging to some who don’t work in this discipline and want to break into this field, but presently work an administrative or sales role that does not require or need such skills.

But there are a variety of admistrative tasks that implimenting R can prove useful- even at the entry level. What if I told you that you can automate prospecting emails and mail merges with R? This is of particular importance if you are looking to prospect new clients, run a campaign for your organization or network with other businesses and move past introductions and save time.

This blog post is based on personal experience of what I have used for businesses to automate the prospecting process and have successfully avoided their spam folder to my knowledge (i.e. all prospects I reached out to got back to me).

NOTE: There are posts out there for avoiding getting your email blocked (see here). But in this post we are going to look at how to Automate Mail merges ( or Email Blasts)

The problem:

To keep things simple, suppose I have gathered a list of leads with names and emails which for emailing purposes is enough data to make a simple mail merge (or email blast).

(Of course, for our case- this data is made up)

buisness_owners <- data.frame(Name=c("Aaron","Susan","Tim"),
                              Email=c("[email protected]", "[email protected]","[email protected]"))


##    Name                        Email
## 1 Aaron [email protected]
## 2 Susan           [email protected]
## 3   Tim               [email protected]

Using the emayili package

Andrew Collier authored the emayili package and is in my opinion the easiest package to use for sending emails in R. Check out the Github for the package here where he breaks down the specifics of using the package and how each piece works.

Step 1: Installing the package

emayili is now on CRAN so you can install it directly from there.

If you want to get the development version, you can download it using devtools::install_github()

# Option 1: Download the `emayili` package from CRAN


# Option 2: Download the emayili package from Github with the `devtools` package 



# Or to get the package from the development branch


Step 2: writing your email

Because there are many customizable options for writing an email with emayili, I have put together a function called write_email() which contains the arguments which I wanted. You could just pipe (%>%) everything together and not have a function, but for our script – having a pre-built function makes things easier IMHO.

There are definitely better ways to do this, but here is what worked for me!

The function:


                      opening_line= "Dear",


  #Using emayili

    text(content =all_text)


The first four arguments are self explanitory. The opening_line argument I have set as default as "Dear" which is essentially a introduction line that you can have to address your recipient. You can change it to "Hello" or whatever you like.

The body_text argument is the standard text you would like to include. This is all put together into the all_text variable embedded in the function and passed into the the text() function which was chained to the rest of the emayili functions.

Using mapply() to implement the merge:

  email<-write_email(sender="[email protected]",
                     reciever_name = name,
                     subject_line='Personal from R',
                       "\nThanks for letting me spam your email with my robot \n\nBest,\n\nBenjamin")

  # Instalize STMP server
  smtp <- server(host = "",
                 port = 587,
                 username = "[email protected]",
                 password = '********')

  smtp(email, verbose = TRUE)

  # Added System Sleep to Not slam the server with requests and to 
  # avoid an ip-ban.

  Sys.Sleep(runif(1, max=5))

To send the email we need to instalize a STMP server. Based on the host you will be using your port will vary. Some quick googling can help you figure out your appropriate host and port (or you can just check here).

After running this script your blast should be sucessful!

And there you have it!


Breaking into tech can be tough. Just because you are not in tech doesn’t mean you can’t use a programming language like R to help with your work- even if you work in sales or admin.

I hope this blog opens your mind to using R beyond statistical analysis.

Let me know what you think!

To leave a comment for the author, please follow the link and comment on their blog: r – bensstats. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)