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:
brew install r
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
install.packages function. Run the RScript commands below to install all the packages we need:
Rscript -e 'install.packages("sendmailR",repos="http://cran.r-project.org")' Rscript -e 'install.packages("httr",repos="http://cran.r-project.org")' Rscript -e 'install.packages("blastula",repos="http://cran.r-project.org")'
This will make our desired packages available in our R code when calling the
Send email with sendmailR
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
sendmailR 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
blastula package covered in the next section.
Create a new R script and import the
sendmailR library plus
httr for http requests.
Then send using the
# Now send an email to the address from <- "<firstname.lastname@example.org>" to <- address subject <- "Send email with R!" body <- "Wow, R can do everything." sendmail(from,to,subject,body,control=list(smtpServer=host,smtpPort=port))
Send with Blastula
Another great package for emailing in Rlang is
blastula. In contrast to
sendmailR 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:
email <- compose_email( body = md(c("Test email")) ) print (paste("Enter password when prompted", password)) smtp_send( email = email, from = email_address, to = email_address, credentials = creds( host = host, port = port, user = username ) )
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.
Test emails safely with no risk of spamming users with real emails
MailSlurp Java SDK for sending and receive email and attachments on the JVM.
How to send email in CSharp, PHP, Ruby, Java, Python, and NodeJS using the SMTP protocol and MailSlurp mailservers.
1111 SMTP Connections failing
Send emails using SMTP mailer in Python. Detailed tutorial for sending mail in Python.
JS SMTP email sending guide using HTML and NodeJS.
PHPMailer tutorial (Sending email with PHP)
Use Send-MailMessage in Windows Powershell to send emails using an SMTP server or MailSlurp's free email API.
RLang tutorial for sending emails over SMTP.
A full list of commands and response status codes for SMTP mail servers when sending emails.
How to use Telnet to test the SMTP servers?
Amazon SES is an email service among virtual servers, AI apps, and libraries. Ideally, you can integrate your email system with your program hosted on Amazon.
What are common SMTP mail server ports and how do you find them?
Picking the right port for SMTP mailservers
How to send emails that don't trigger a spam warning.
What is cc and bcc in email? What is the difference between cc and bcc?
What is BCC email field (blind carbon copy) and how does it differ to CC?
MailServers use TCP ports to transmit emails. Picking the right port between 25 vs 2555, 465 vs 587 in SMTP is easy with this guide.
How to setup MailSlurp with Outlook, Gmail, Thunderbird, Mail app and more.
Create a custom SMTP client and access MailSlurp inboxes from CSharp/DotNET.
DMARC – All Facts that you should know
Email size limits
How to manage email subdomain custom mailboxes using SMTP/IMAP mailserver settings.
Send email in Visual basic
A guide to PHP Mailer
How to access MailSlurp email accounts using IMAP and SMTP servers
How to use SMTP relay to address spam and security issues that affect outbound mail servers.
The difference between HTTP and SMTP inboxes and domains in MailSlurp.
What are SMTP ports
How to use CSharp SMTP client to send email with MailSlurp mail server
How to use Java SMTP client to send email with MailSlurp mail server on the JDK
Use PHPMailer to send emails with SMTP and MailSlurp
How to send emails with Python's built-in SMTP client and MailSlurp mail servers.
How to use Ruby SMTP client to send email with the MailSlurp mail servers