mail_merge: Merges data into an email and send.

Description Usage Arguments Value See Also Examples

View source: R/mail_merge.R

Description

[Experimental]

Merges columns from a data frame into a markdown document using the glue::glue_data() function. The markdown can contain a yaml header for subject and cc line.

Note that only 'gmail' is supported at the moment, via gmailr::gm_send_message.

Before using mail_merge(), you must be authenticated to the gmail service. Use gmailr::gm_auth() to authenticate prior to starting the mail merge.

Usage

1
2
3
4
5
6
7
8
9
mail_merge(
  data,
  message,
  to_col = "email",
  send = c("preview", "draft", "immediately"),
  confirm = FALSE,
  sleep_preview = 1,
  sleep_send = 0.1
)

Arguments

data

A data frame or tibble with all the columns that should be glued into the message. Substitution is performed using glue::glue_data()'

message

A list with components yaml and body. You can use mm_read_message() or mm_read_message_googledoc() to construct a message in this format.

to_col

The name of the column in data that contains the email address to send the message to.

send

A character string, one of:

  • "preview" : displays message in viewer without sending mail

  • "draft : writes message into "drafts" folder on gmail

  • "immediately" : sends email

confirm

If TRUE sends email without additional confirmation. If FALSE asks for confirmation before sending.

sleep_preview

If send == "preview" the number of seconds to sleep between each preview. See also preview_mailmerge

sleep_send

If send == "immediately" the number of seconds to sleep between each email send (to prevent gmail API 500 errors).

Value

Returns a list for every message, consisting of:

See Also

preview_mailmerge

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
## ---- input-data --------------------------------------------------------

dat <-  data.frame(
  email      = c("friend@example.com", "foe@example.com"),
  first_name = c("friend", "foe"),
  thing      = c("something good", "something bad"),
  stringsAsFactors = FALSE
)

## ---- markdown-message --------------------------------------------------

msg <- '
---
subject: "**Hello, {first_name}**"
---

Hi, **{first_name}**

I am writing to tell you about **{thing}**.

{if (first_name == "friend") "Regards" else ""}


Me
'


## ---- mail-merge --------------------------------------------------------

dat %>% 
  mail_merge(msg)

if (interactive()) {
  dat %>%
    mail_merge(msg) %>%
    print()
}

## ---- display shiny gadget ----------------------------------------------

if (interactive()) {
  dat %>%
    mail_merge(msg) %>%
    preview_mailmerge()
}

mailmerge documentation built on May 11, 2021, 5:06 p.m.