I have an R script that I want to automatically send an email using Microsoft Outlook after it has finished completing. I'm using the "RDCOMClient" package and I want to add multiple attachments to the email.
Here's the code that I'm trying to use:
library(RDCOMClient)
OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)
outMail[["To"]] = paste("[email protected]","[email protected]", sep=";", collapse=NULL)
outMail[["subject"]] = "some subject"
outMail[["body"]] = "some body"
outMail[["attachments"]]$Add("C:/Path/To/The/Attachment/File.ext")
outMail$Send()
I tried using paste for the attachments like the "To" option but I'm 99% sure that is what broke the attachments because it works with just one. It works perfectly for adding multiple recipients. Does anyone know how I can add multiple attachments with this package?
Just add another attachment line:
outMail[["attachments"]]$Add("C:/Path/To/The/Attachment/File.ext")
outMail[["attachments"]]$Add("C:/Path/To/The/Attachment/File2.ext")
Or map(loop) over an attachment object:
attachments <- c("C:/Path/To/The/Attachment/File.ext",
"C:/Path/To/The/Attachment/File2.ext")
purrr::map(attachments, ~ outMail[["attachments"]]$Add(.))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With