The {emayili}
package has adapters which make it simple to send email via a variety of services. For example, it caters specifically for ZeptoMail, MailerSend, Mailfence and Sendinblue. The latest version of {emayili}
, 0.8.0 published on 23 April 2024, adds an adapter for Mailtrap.
Account Setup
Before you can send emails via Mailtrap you will need to create an account. Mailtrap offers both an API and SMTP servers. Finding the SMTP credentials on Mailtrap is not entirely simple. Well, it wasn’t for me. Select Sending Domains in the sidebar and then open the SMTP/API Settings tab.
On that page you’ll have access to the credentials for the transactional and bulk message streams.
There’s also a testing sandbox, and you can find the credentials for that by selecting Email Testing and Inboxes in the sidebar. Then open the SMTP Settings tab and choose one of the integrations. With the Postfix integration selected it’s simple to find the username and password.
Sending Mail via Mailtrap
Load {emayili}
and create a Mailtrap SMTP server object. I’m storing my Mailtrap username and password in environment variables MAILTRAP_USERNAME
and MAILTRAP_PASSWORD
.
library(emayili)
MAILTRAP_USERNAME <- Sys.getenv("MAILTRAP_USERNAME")
MAILTRAP_PASSWORD <- Sys.getenv("MAILTRAP_PASSWORD")
smtp <- mailtrap(
username = MAILTRAP_USERNAME,
password = MAILTRAP_PASSWORD
)
Now create a message object. The address specified in the From
field needs to be the same one that was registered with Mailtrap (see Account Setup above).
MAILTRAP_ADDRESS <- Sys.getenv("MAILTRAP_ADDRESS")
msg <- envelope(
to = "alice@gmail.com",
from = MAILTRAP_ADDRESS,
subject = "Test"
) %>%
text("Hello there!")
Now send that message.
smtp(msg, verbose = TRUE)
Sandbox & Bulk Messaging
You can use the Sandbox and Bulk Messaging features by activating the sandbox
and bulk
arguments.
# Sandbox SMTP server.
sandbox <- mailtrap(sandbox = TRUE, ...)
# SandBulk Messaging SMTP server.
sandbox <- mailtrap(bulk = TRUE, ...)
If you are using another email service and you’d like it to be specifically supported by {emayili}
, just create an issue and I’ll get it sorted.