0

I currently have 5 spreadsheets that I need to email out separately into 5 emails.

This is the logic I currently have for the email:

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

How would I iterate over this to send out 5 separate emails while also attaching the relevant spreadsheet?

Jaskeil
  • 1,044
  • 12
  • 33

1 Answers1

1

You haven't described your spreadsheet naming.

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

}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
CALUM Polwart
  • 497
  • 3
  • 5
  • Thank you this is close to what I need but the issue I encountered is that it attached all the spreadsheets to one email. What I was hoping for it to send a separate email per attachment. – Jaskeil Jun 07 '21 at 16:31