使用 For 循环从 R 发送电子邮件?使用 RDCOMClient

Sending an Email from R Using For Loop? Using RDCOMClient

我目前有 5 个电子表格,需要分别发送到 5 个电子邮件中。

这是我目前的邮件逻辑:

#Script to send out
attachments <- c('C:/Users/santi/Documents/Cost Changes xlsx/spreadsheet.xlsx') #spreadsheet variable
OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)
outMail[["To"]] = paste("john_doe@outlook.com"
                        , sep=";", collapse=NULL)
outMail[["subject"]] = " Rebate"
outMail[["body"]] = "Hi - 

Attached is the spreadsheet

Let me know if you have any questions, thanks.

This is an automated message from RStudio please respond if you find any errors.

"
purrr::map(attachments, ~ outMail[["attachments"]]$Add(.))
outMail$Send()

我将如何对此进行迭代以发送 5 封单独的电子邮件,同时还附上相关电子表格?

您没有描述电子表格的命名方式。

SheetNames <- c("sheet1.xls", "sheet2.xls", "sheet3.xls")
PathName <- "C:/Users/santi/Documents/Cost Changes xlsx/"

for (sheet in SheetNames) {

attachments = c(paste0(PathName, sheet))

OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)
outMail[["To"]] = paste("john_doe@outlook.com"
                        , sep=";", collapse=NULL)
outMail[["subject"]] = " Rebate"
outMail[["body"]] = "Hi - 

Attached is the spreadsheet

Let me know if you have any questions, thanks.

This is an automated message from RStudio please respond if you find any errors.

"
purrr::map(attachments, ~ outMail[["attachments"]]$Add(.))
outMail$Send()

}