{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!")
Date:                      Fri, 17 Sep 2021 07:02:11 GMT
X-Mailer:                  {emayili}-0.5.0
MIME-Version:              1.0
Content-Type:              text/plain; charset=utf-8
Content-Disposition:       inline
Content-Transfer-Encoding: 7bit

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}!")
Date:                      Fri, 17 Sep 2021 07:02:11 GMT
X-Mailer:                  {emayili}-0.5.0
MIME-Version:              1.0
Content-Type:              text/plain; charset=utf-8
Content-Disposition:       inline
Content-Transfer-Encoding: 7bit

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:                      Fri, 17 Sep 2021 07:02:11 GMT
To:                        alice@google.com
X-Mailer:                  {emayili}-0.5.0
MIME-Version:              1.0
Content-Type:              text/plain; charset=utf-8
Content-Disposition:       inline
Content-Transfer-Encoding: 7bit

Hello {name}!Date:                      Fri, 17 Sep 2021 07:02:11 GMT
To:                        bob@yahoo.com
X-Mailer:                  {emayili}-0.5.0
MIME-Version:              1.0
Content-Type:              text/plain; charset=utf-8
Content-Disposition:       inline
Content-Transfer-Encoding: 7bit

Hello {name}!

Unleash your inner spam king.