{emayili} Interpolating Message Content

[This article was first published on R | datawookie, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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, 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--

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--

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,
  "[email protected]", "Alice",
  "[email protected]", "Bob"
) %>%
  pwalk(function(email, name) {
    email <- envelope(to = email) %>% text("Hello {name}!")
    print(email)
  })
Date:         Fri, 03 Sep 2021 09:07:10 GMT
To:           [email protected]
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:           [email protected]
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--

Unleash your inner spam king.

To leave a comment for the author, please follow the link and comment on their blog: R | datawookie.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)