Sending Emails with blastula
Introduction
The blastula package provides a simple interface for sending emails from R. It supports SMTP configuration, HTML and plain text messages, inline images, and file attachments. This guide covers the essentials for integrating email functionality into your R workflows.
Installation
Install blastula from CRAN:
install.packages("blastula")
Load the package:
library(blastula)
Configuring SMTP
Before sending emails, configure your SMTP credentials. The creds_envvars() function reads credentials from environment variables, or use creds_file() to load from a file.
# Using environment variables
smtp_creds <- creds_envvars(
user = Sys.getenv("SMTP_USER"),
password = Sys.getenv("SMTP_PASSWORD"),
host = Sys.getenv("SMTP_HOST"),
port = as.integer(Sys.getenv("SMTP_PORT"))
)
# Using a credentials file (editable file in home directory)
smtp_creds <- creds_file("~/.Renviron")
# # Edit the file to contain:
# SMTP_USER=your-email@example.com
# SMTP_PASSWORD=your-password
# SMTP_HOST=smtp.example.com
# SMTP_PORT=587
Common SMTP settings for popular providers:
- Gmail: host = “smtp.gmail.com”, port = 587
- Outlook: host = “smtp.office365.com”, port = 587
- Amazon SES: host = “email-smtp.us-east-1.amazonaws.com”, port = 587
Composing Emails
The compose_email() function creates an email object. You can specify plain text, HTML content, or both.
# Plain text email
email_text <- compose_email(
body = "Hello, this is a plain text message."
)
# HTML email
email_html <- compose_email(
body = md("
# Hello
This is an **HTML** message with formatted content.
- Point one
- Point two
")
)
# Email with both plain text and HTML
email_both <- compose_email(
body = "Hello, this is the plain text version.",
body_html = md("Hello, this is the **HTML** version.")
)
The md() function marks content as markdown, which blastula converts to HTML.
Adding Attachments
Use the add_attachment() function to attach files:
# Create email with attachment
email_attachment <- compose_email(
body = "Please find the attached report."
) %>%
add_attachment(
file = "report.pdf",
mime_type = "application/pdf"
)
# Or use a path
email_attachment <- compose_email(
body = "Attached is the data file."
) %>%
add_attachment(
file = "/path/to/data.csv",
mime_type = "text/csv"
)
Adding Inline Images
Embed images directly in HTML emails using add_image():
email_image <- compose_email(
body = md("
# Report

See the chart above for details.
")
) %>%
add_image(
file = "chart.png",
image_id = "chart-image"
)
The image_id must match the content ID in your markdown (cid:chart-image).
Sending Emails
The smtp_send() function delivers your email:
# Send the email
smtp_send(
email = email_html,
from = "sender@example.com",
to = "recipient@example.com",
subject = "Email Subject Line",
credentials = smtp_creds
)
Send to multiple recipients:
smtp_send(
email = email_both,
from = "sender@example.com",
to = c("recipient1@example.com", "recipient2@example.com"),
cc = "copy@example.com",
bcc = "hidden@example.com",
subject = "Multiple Recipients",
credentials = smtp_creds
)
Sending to Multiple Recipients in a Loop
For sending personalized emails to a list of recipients:
# Data frame with recipient information
recipients <- data.frame(
name = c("Alice", "Bob", "Charlie"),
email = c("alice@example.com", "bob@example.com", "charlie@example.com"),
amount = c(100, 250, 75)
)
# Send personalized emails
for (i in seq_len(nrow(recipients))) {
email <- compose_email(
body = md(paste0(
"Dear ", recipients$name[i], ",\n\n",
"Your account balance is $", recipients$amount[i], ".\n\n",
"Thank you for your business."
))
)
smtp_send(
email = email,
from = "noreply@example.com",
to = recipients$email[i],
subject = paste0("Account Update - ", recipients$name[i]),
credentials = smtp_creds
)
# Add delay to avoid rate limiting
Sys.sleep(1)
}
Error Handling
Wrap sending in tryCatch for safe error handling:
send_email_safely <- function(email, from, to, subject, creds) {
tryCatch(
{
smtp_send(
email = email,
from = from,
to = to,
subject = subject,
credentials = creds
)
message("Email sent successfully to ", to)
return(TRUE)
},
error = function(e) {
warning("Failed to send email to ", to, ": ", e$message)
return(FALSE)
}
)
}
Testing Configuration
Use test_smtp() to verify your SMTP configuration works:
test_smtp(
creds = smtp_creds,
from = "test@example.com",
to = "test@example.com"
)
This sends a test email through your configured server.
Security Considerations
- Store SMTP credentials in environment variables or a secure credentials file
- Never hardcode passwords in scripts
- Use application-specific passwords if your email provider supports them
- Consider using two-factor authentication with app passwords
- For production use, restrict SMTP access to specific IP addresses when possible
See Also
- base-paste — Combine strings for email content
- base-cat — Output text and messages
- dplyr-filter — Filter data before sending bulk emails