我已经弄清楚了如何通过带有附件的R发送单个电子邮件,并显示电子邮件的正文(似乎是一个常见问题)。不过,我想遵循Jenny Bryan's method发送多封电子邮件,同时附加一个文件。
发送一封带有附件和消息的电子邮件。
msg <- "this is the message of the email"
test_email <- mime() %>%
to("to@gmail.com") %>%
from("from@gmail") %>%
subject("subject goes here") %>%
body(msg) %>%
attach_file("29697.html", type = "html")
test_email <- attach_part(test_email, msg)
send_message(test_email)
为了模仿上面的代码,但以詹妮为例,我有以下内容:
addresses <- read_csv("addresses.csv") #contains a column for 'name' and 'email'
email_sender <- 'First Last <from@gmail.com>' # your Gmail address
msg <- "this is the message of the email"
edat <- addresses %>%
mutate(
To = sprintf('%s <%s>', name, email),
From = email_sender,
Subject = sprintf('Mark for %s', name),
body = msg,
attach_file = sprintf('%s.html, type = html', name))
edat <- edat %>%
select(To, From, Subject, body, attach_file)
emails <- edat %>%
pmap(mime)
safe_send_message <- safely(send_message)
sent_mail <- emails %>%
map(safe_send_message)
以上示例创建了一个列表,以形成gmailR使用的mime
文件结构的组件,但是,它没有像上面的单个示例那样附加文件。我试图类似地构造attach_file
函数,但是,它没有像单独调用它那样将其放在mime的列表项中,就像上面的单个示例一样,将其放在{{ 1}}部分。预先感谢任何人遇到此问题。
答案 0 :(得分:0)
有时候,编写一个函数对一行小标题执行感兴趣的操作可能会更容易(尽管可能不那么优雅)。然后使用map/map2/pmap
或walk/walk2/pwalk
重复应用该功能。
library("tidyverse")
library("gmailr")
# Create two empty attachment files needed for example
file.create("A.html")
#> [1] TRUE
file.create("B.html")
#> [1] TRUE
test_send_message <- function(msg) {
# Doesn't actually send any emails
print(strwrap(as.character(msg)))
}
prepare_and_send <- function(sender, recepient,
title, text,
attachment) {
email <- mime() %>%
to(sender) %>%
from(recepient) %>%
subject(title) %>%
text_body(text) %>%
attach_file(attachment, type = "html") %>%
test_send_message()
}
# Test that function works to send one email
prepare_and_send("to@gmail.com", "from@gmail", "some subject",
"some text", "A.html")
#> [1] "MIME-Version: 1.0\r Date: Mon, 18 Mar 2019 21:32:20 GMT\r To:"
#> [2] "to@gmail.com\r From: from@gmail\r Subject: some subject\r"
#> [3] "Content-Type: multipart/mixed;"
#> [4] "boundary=2c47d060a267bfdca54a1fdfe0c4ecaf\r Content-Disposition:"
#> [5] "inline\r \r MIME-Version: 1.0\r Date: Mon, 18 Mar 2019 21:32:20 GMT\r"
#> [6] "Content-Type: text/plain; charset=utf-8; format=flowed\r"
#> [7] "Content-Transfer-Encoding: quoted-printable\r Content-Disposition:"
#> [8] "inline\r \r some text\r --2c47d060a267bfdca54a1fdfe0c4ecaf\r"
#> [9] "MIME-Version: 1.0\r Date: Mon, 18 Mar 2019 21:32:20 GMT\r"
#> [10] "Content-Type: html; name=A.html\r Content-Transfer-Encoding:"
#> [11] "base64\r Content-Disposition: inline; filename=A.html;"
#> [12] "modification-date=Mon, 18 Mar 2019 21:32:20 GMT\r \r \r"
#> [13] "--2c47d060a267bfdca54a1fdfe0c4ecaf--\r"
# Then try to send multiple emails
addresses <- tribble(
~name, ~email,
"A", "a@gmail.com",
"B", "b@gmail.com"
)
email_sender <- 'First Last <from@gmail.com>'
msg <- "this is the message of the email"
addresses %>%
mutate(
To = sprintf('%s <%s>', name, email),
From = email_sender,
Subject = sprintf('Mark for %s', name),
body = msg,
attachment = sprintf('%s.html', name)) %>%
mutate(x = pmap(list(To, From, Subject, body, attachment),
safely(prepare_and_send)))
#> [1] "MIME-Version: 1.0\r Date: Mon, 18 Mar 2019 21:32:20 GMT\r To: A"
#> [2] "<a@gmail.com>\r From: First Last <from@gmail.com>\r Subject: Mark"
#> [3] "for A\r Content-Type: multipart/mixed;"
#> [4] "boundary=bd435d3f3bb00aee58b776142a512702\r Content-Disposition:"
#> [5] "inline\r \r MIME-Version: 1.0\r Date: Mon, 18 Mar 2019 21:32:20 GMT\r"
#> [6] "Content-Type: text/plain; charset=utf-8; format=flowed\r"
#> [7] "Content-Transfer-Encoding: quoted-printable\r Content-Disposition:"
#> [8] "inline\r \r this is the message of the email\r"
#> [9] "--bd435d3f3bb00aee58b776142a512702\r MIME-Version: 1.0\r Date: Mon,"
#> [10] "18 Mar 2019 21:32:20 GMT\r Content-Type: html; name=A.html\r"
#> [11] "Content-Transfer-Encoding: base64\r Content-Disposition: inline;"
#> [12] "filename=A.html; modification-date=Mon, 18 Mar 2019 21:32:20 GMT\r"
#> [13] "\r \r --bd435d3f3bb00aee58b776142a512702--\r"
#> [1] "MIME-Version: 1.0\r Date: Mon, 18 Mar 2019 21:32:20 GMT\r To: B"
#> [2] "<b@gmail.com>\r From: First Last <from@gmail.com>\r Subject: Mark"
#> [3] "for B\r Content-Type: multipart/mixed;"
#> [4] "boundary=aa10620651c2281647a4fba0dc392694\r Content-Disposition:"
#> [5] "inline\r \r MIME-Version: 1.0\r Date: Mon, 18 Mar 2019 21:32:20 GMT\r"
#> [6] "Content-Type: text/plain; charset=utf-8; format=flowed\r"
#> [7] "Content-Transfer-Encoding: quoted-printable\r Content-Disposition:"
#> [8] "inline\r \r this is the message of the email\r"
#> [9] "--aa10620651c2281647a4fba0dc392694\r MIME-Version: 1.0\r Date: Mon,"
#> [10] "18 Mar 2019 21:32:20 GMT\r Content-Type: html; name=B.html\r"
#> [11] "Content-Transfer-Encoding: base64\r Content-Disposition: inline;"
#> [12] "filename=B.html; modification-date=Mon, 18 Mar 2019 21:32:20 GMT\r"
#> [13] "\r \r --aa10620651c2281647a4fba0dc392694--\r"
#> # A tibble: 2 x 8
#> name email To From Subject body attachment x
#> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <list>
#> 1 A a@gmai~ A <a@gm~ First Last~ Mark f~ this is the~ A.html <list~
#> 2 B b@gmai~ B <b@gm~ First Last~ Mark f~ this is the~ B.html <list~
由reprex package(v0.2.1)于2019-03-18创建
答案 1 :(得分:0)
我使用了dipetkov's建议中的代码,我对其进行了修改,使其既包含附件,又包含发送单个电子邮件的消息。
library("tidyverse")
library("gmailr")
msg <- "this is the message of the email"
prepare_and_send <- function(sender, recipient,
title, text,
attachment) {
email <- mime() %>%
to(recipient) %>%
from(sender) %>%
subject(title) %>%
html_body(text) %>%
attach_file(attachment, type = "html")
email <- attach_part(email, msg) %>%
send_message()
}
# Test that function works to send one email
prepare_and_send("sender@gmail", "to@gmail", "some subject",
"some text", "20558.html")
再进一步,我对其进行了一些修改,以遍历“地址”数据框中保存的一系列电子邮件。
#Iterate it ----
addresses %>%
mutate(
to = sprintf('%s <%s>', name, email),
from = email_sender,
subject = sprintf('Mark for %s', name),
html_body = msg,
attachment = sprintf('%s.html', name)) %>%
mutate(x = pmap(list(from, to, subject, html_body, attachment),
safely(prepare_and_send)))