{emayili} Interpolating Message Content

A small new feature added to {emayili}: the ability to interpolate content into the email message body.

Load Package

Load the {emayili} package and create a message skeleton.

library(emayili)

options(
  # Always print message body.
  envelope_details = TRUE,
  # Print message from pipeline.
  envelope_invisible = FALSE
)

# Create a message skeleton.
#
email <- envelope()

Body

Now let’s look at different ways to add a text body.

If you know precisely what your message is going to say, then you can just write it straight into the message body.

email %>% text("Hello Alice!")

But what if a part of the message is stored in a variable? Use the {glue} syntax for variable interpolation.

name <- "Bob"

email %>% text("Hello {name}!")

This should make generating automated (yet personalised!) emails a lot simpler. For example, here’s how you’d go about creating emails from a data frame with columns holding names and email addresses.

tribble(
  ~email, ~name,
  "alice@google.com", "Alice",
  "bob@yahoo.com", "Bob"
) %>%
  pwalk(function(email, name) {
    email <- envelope(to = email) %>% text("Hello {name}!")
    print(email)
  })
Date:                        Sun, 09 Jan 2022 05:26:58 GMT
X-Mailer:                    {emayili}-0.7.1
MIME-Version:                1.0
To:                          alice@google.com
Date:                        Sun, 09 Jan 2022 05:26:58 GMT
X-Mailer:                    {emayili}-0.7.1
MIME-Version:                1.0
To:                          bob@yahoo.com

Unleash your inner spam king.


The `{emayili}` package is developed & supported by [Fathom Data](https://www.fathomdata.dev).