Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emailing multiple attachments using RDCOMClient and Outlook

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?

like image 753
David Avatar asked Mar 25 '26 09:03

David


1 Answers

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(.))
like image 164
Tunn Avatar answered Mar 27 '26 00:03

Tunn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!