DEV Community

Andrew B. Collier
Andrew B. Collier

Posted on • Originally published at datawookie.dev on <time datetime="2021-09-03T00:00:00Z" class="date-no-year">Sep 3</time>

{emayili} Interpolating Message Content

{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()
Enter fullscreen mode Exit fullscreen mode

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, 03 Sep 2021 09:07:10 GMT
X-Mailer: {emayili}-0.4.17
MIME-Version: 1.0
Content-type: multipart/mixed; boundary="1133f2d1f303af212e1f293e2c361c"

--1133f2d1f303af212e1f293e2c361c
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: 7bit

Hello Alice!

--1133f2d1f303af212e1f293e2c361c--
Enter fullscreen mode Exit fullscreen mode

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, 03 Sep 2021 09:07:10 GMT
X-Mailer: {emayili}-0.4.17
MIME-Version: 1.0
Content-type: multipart/mixed; boundary="1133f2d1f303af212e1f293e2c361c"

--1133f2d1f303af212e1f293e2c361c
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: 7bit

Hello Bob!

--1133f2d1f303af212e1f293e2c361c--
Enter fullscreen mode Exit fullscreen mode

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, 03 Sep 2021 09:07:10 GMT
To: alice@google.com
X-Mailer: {emayili}-0.4.17
MIME-Version: 1.0
Content-type: multipart/mixed; boundary="153ce203c1ff10232f22237eb5"

--153ce203c1ff10232f22237eb5
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: 7bit

Hello Alice!

--153ce203c1ff10232f22237eb5--
Date: Fri, 03 Sep 2021 09:07:10 GMT
To: bob@yahoo.com
X-Mailer: {emayili}-0.4.17
MIME-Version: 1.0
Content-type: multipart/mixed; boundary="92832121424fb5133d2a1220371b"

--92832121424fb5133d2a1220371b
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: 7bit

Hello Bob!

--92832121424fb5133d2a1220371b--
Enter fullscreen mode Exit fullscreen mode

Unleash your inner spam king.

Discussion (0)