使用SendGrid从R发送电子邮件时如何添加抄送

时间:2019-09-06 15:14:22

标签: r email sendgrid

我想从R发送电子邮件,其中在“ to”和“ cc”中有许多地址

按照此示例https://www.r-bloggers.com/automatically-write-and-email-reports-with-r-sendgrid-displayr/,我可以将电子邮件发送到1个地址。

经过一些实验,我使用这样的代码将电子邮件发送给“收件人”中的少数读者(实际的API密钥和电子邮件已删除)

library(httr)
library(jsonlite)

body = paste("\\nDear friend,",
             "\\n\\n",
             "\\n\\nI'm testing email.",
             "\\n\\n",
             "\\n\\nKind regards,",
             "\\n\\nYuriy",sep="")


key1       <-  "SG.****" #enter your API Key here

readers <- c("m1@example.com",  "m2@example.com")
copiers <- c("m3@example.com",  "m4@example.com")


to.email   <-  paste0('{\"email\": \"', 
                      paste( readers, collapse = '\"}, {\"email\": \"') , 
                      '\"}')
cc.email   <-  paste0(' {\"email\": \"',
                      paste(copiers, collapse = '\"}}, {\"cc\":{\"email\": \"'), 
                      '\"}')
from.email <-  "from@example.com"
subject    <-  "Testing Sendgrid"
message.body  <-  body


msg  <- sprintf('{\"personalizations\":
        [{\"to\": [ %s]}],
          \"from\": {\"email\": \"%s\"},
          \"subject\": \"%s",
          \"content\": [{\"type\": \"text/plain\", \"value\": \"%s\"}]
            }', 
                to.email, from.email, subject, message.body)

pp <-  POST("https://api.sendgrid.com/v3/mail/send",
          body = msg,
          config = add_headers("Authorization" = sprintf("Bearer %s", key1),
                               "Content-Type" = "application/json"),
          verbose())

但是我不能在“抄送”中添加几封电子邮件。 阅读文档https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html后,它在下面尝试了代码,但是我收到了“错误请求”答复。

msg  <- sprintf('{\"personalizations\":
        [{\"to\": [ %s]}],
        [{\"cc\": [ %s]}],
          \"from\": {\"email\": \"%s\"},
          \"subject\": \"%s",
          \"content\": [{\"type\": \"text/plain\", \"value\": \"%s\"}]
            }', 
                to.email, cc.email, from.email, subject, message.body)

我看到了其他语言的解决方案,但没有R的解决方案。 有什么想法吗?

1 个答案:

答案 0 :(得分:1)

1)使用sendgrid

对于sendgrid,我们不需要使用/来转义每个字符串,而是自动完成的。正确构造请求对象也很重要。

library(httr)
library(jsonlite)

key1 <- "SG.***"
body = paste0("\\nDear friend,",
          "\\n\\n",
          "\\n\\nI'm testing email.",
          "\\n\\n",
          "\\n\\nKind regards,",
          "\\n\\nRonak")


readers <- c("email1@example.com", "email2@example.com")
copiers <- c("email3@example.com", "email4@example.com")
to.email <- paste0('{"email": "',paste(readers,collapse = '"}, {"email": "'),'"}')
cc.email <- paste0('{"email": "',paste(copiers,collapse = '"}, {"email": "'),'"}')
from.email <-  "test@example.com"
subject    <-  "Testing Sendgrid"
message.body  <-  body


msg  <- sprintf('{"personalizations":[{
                  "to": [ %s],
                  "cc": [ %s]
                  }],
                 "from\": {"email": "%s"},
                 "subject\": "%s",
                 "content\": [{"type": "text/plain", "value": "%s"}]
                }', to.email, cc.email, from.email, subject, message.body)

POST("https://api.sendgrid.com/v3/mail/send",
 body = msg,
 config = add_headers("Authorization" = sprintf("Bearer %s", key1),
                      "Content-Type" = "application/json"),
 verbose())

2)使用emayili

R中除了使用sendgrid外,还有许多用于发送电子邮件的软件包。我使用了一个新的软件包emayili发送电子邮件,它对我有用。

之所以选择它,是因为:-

  • 该软件包是相当新的,因此克服了旧软件包中存在的问题。以前的软件包在安装和设置上有很多困难。

  • 使用它非常简单易用的语法

但是,缺点是,由于它是一个相当新的东西,它在CRAN上仍然不可用,并且需要从github下载该软件包。

install.packages("curl")
remotes::install_github("datawookie/emayili")

library(emayili)
library(magrittr)
library(curl)


email <- envelope()
email <- email %>%
         from(c("email1@gmail.com", "email4@gmail.com")) %>%
         to("email2@gmail.com") %>%
         cc("email3@yahoo.com")

email <- email %>% subject("This is a plain text message!")
email <- email %>% body("Hello!")
smtp <- server(host = "smtp.gmail.com",
           port = 587,
           username = "email1@gmail.com",
           password = "password")
smtp(email, verbose = TRUE)

我在自己的gmail和yahoo帐户上对此进行了测试,邮件通过了。如果您不想用日志消息淹没控制台,则可能要添加verbose = FALSE。另外,如果您使用的是gmail,则可能需要allow access to less secure apps发送电子邮件。