Home > Software engineering >  R: PDF as attachment in email automation
R: PDF as attachment in email automation

Time:04-21

Need to ask a basic question. I would like to send automated emails with PDF in attachment with R Studio. However, when I run the the outMail line for the attachment I get the following error:

<checkErrorInfo> 80020009 
No support for InterfaceSupportsErrorInfo
checkErrorInfo -2147352567
Errore: Eccezione.

I 100% sure that the problems relies in the line of outMail, as all the other do not produce any errors and the code works without the attachment or with an attachment that it is not a pdf.

(working with outlook)

Here is the code:

#install.packages("RDCOMClient")
#library("RDCOMClient")

## initiation com api
OutApp <- COMCreate("Outlook.Application")

## create an email 
outMail = OutApp$CreateItem(0)

## configure  email 
outMail[["To"]] = "[email protected]"
outMail[["subject"]] = "some subject"
outMail[["body"]] = "some body"

##send it    
path_to_file = "C:\\Users\\Desktop\\app\\mypdf.pdf"
outMail[["Attachments"]]$Add(normalizePath(path_to_file))

#send email
outMail$Send()

Already tried to put just one \ or to make them in the opposite verse /

CodePudding user response:

The problem is that the path is being created in R, which prefers forward slashes (since the backslash is the escape character), but it's being interpreted by Outlook, which only takes backslashes.

There is no need to use the normalizePath function in the code. For example:

##send it    
path_to_file = "C:\\Users\\Desktop\\app\\mypdf.pdf"
outMail[["Attachments"]]$Add(path_to_file)

R will strip out the escape characters and and pass a clean path to Outlook.

  • Related