如何在生成任何格式(PDF、HTML、WORD)时通过电子邮件发送 R markdown 报告?

How to email an R markdown report in any format (PDF, HTML, WORD) when it is generated?

我有一个 R shiny 应用程序,它可以根据用户点击的内容生成任何格式的 R markdown 报告。 每次生成此报告时,我都想通过电子邮件将其发送给自己 我似乎在网上找不到太多关于这个的信息。我想知道是否有人知道如何开始这个

您可以试试 mailR 包。从 mailR github documentation,您可以发送电子邮件并使用 attach.files 附加相关报告。

library(mailR)
send.mail(from = "sender@gmail.com",
          to = c("recipient1@gmail.com", "recipient2@gmail.com"),
          subject = "Subject of the email",
          body = "Body of the email",
          smtp = list(host.name = "smtp.gmail.com", port = 465, ssl = TRUE,
                      user.name = "gmail_username", passwd = "password"),
          authenticate = TRUE,
          send = TRUE,
          attach.files = c("./download.log"),
          file.names = c("Download log.log"),
          file.descriptions = c("Description for download log"))

sendmailR 可以获得类似的结果,但附件是使用 mime_part().

添加到电子邮件正文中的
library(sendmailR)
from <- 'you@account.com'
to   <- 'recipient@account.com'
subject <- 'Email Subject'
body <- list('Email body text.',
             mime_part(x = 'pathToAttachment', y = 'nameOfAttachment'))
sendmail(from, to, subject, msg = body,
         control = list(smtpServer='ASPMX.L.GOOGLE.COM'))

如果您使用 Outlook,我会推荐 RDCOMClient 包。

install.packages(RDCOMClient)
require(RDCOMClient)

OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)
outMail[["To"]] = "you@domain.com"
outMail[["subject"]] = "subject here"
outMail[["htmlbody"]] = "email text"
outMail[["Attachments"]]$Add("c:/file.blah")
outMail$Send()