【问题标题】:Sending an Email from R Using For Loop? Using RDCOMClient使用 For 循环从 R 发送电子邮件?使用 RDCOMClient
【发布时间】:2021-06-04 20:29:03
【问题描述】:

我目前有 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 封单独的电子邮件,同时附加相关的电子表格?

【问题讨论】:

    标签: r for-loop email automation rdcomclient


    【解决方案1】:

    您尚未描述您的电子表格命名。

    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()
    
    }
    

    【讨论】:

    • 谢谢,这与我需要的很接近,但我遇到的问题是将所有电子表格附加到一封电子邮件中。我希望它为每个附件发送一封单独​​的电子邮件。
    猜你喜欢
    • 2020-03-29
    • 1970-01-01
    • 2015-08-10
    • 2021-12-30
    • 1970-01-01
    • 2019-12-17
    • 2020-02-24
    • 1970-01-01
    • 2017-08-15
    相关资源
    最近更新 更多