问题
When i am using latest version R RDCOMClient package for sending outlook Emails, It is showing up an error : "[[<- defined for objects of type "S4" only for subclasses of environment"
Code for the same:
library(RDCOMClient)
## init com api
OutApp <- COMCreate("Outlook.Application")
## create an email
outMail = OutApp$CreateItem(0)
outMail$GetInspector()
signature = outMail[["HTMLBody"]]
## configure email parameter
outMail[["To"]] = "some@outlook.com"
outMail[["CC"]] <- "Some@outlook.com"
outMail[["subject"]] = "some subject"
outMail[["body"]] = "some body"
outMail[["Attachments"]]$Add("C:\\Users\\Some\\Desktop\\file.csv")
outMail[["HTMLBody"]] = paste0('<p>some body', signature, '</p>')
## send it
outMail$Send()
**Error:**
signature = outMail[["HTMLBody"]]
Error in mget(plabels[hasSubclass], env) : invalid first argument
## configure email parameter
outMail[["To"]] = "some@outlook.com"
Error in `[[<-`(`*tmp*`, "To", value = "some@outlook.com") :
[[<- defined for objects of type "S4" only for subclasses of environment
回答1:
I think the below code should work for you. You may have to define body
of the email separately as I have done and then paste it at outMail[["HTMLbody"]] = paste0("<p>", body, "</p>", Signature)
as seen below. This code works without any errors provided you have installed the RDCOMClient
package. I have tested this code using the latest version of R (V3.4.2), RDCOMClient and RStudio. Let me know if this helps you.
library(RDCOMClient)
OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)
# Get signature from outlook
# GetInspector renders the message in html format.
# Note that if you have not created any signatures, this will return blank
outMail$GetInspector()
Signature <- outMail[["HTMLbody"]]
# Define the body of you email separately
body <- "Define your body here."
outMail[["To"]] = "test@test.com"
outMail[["subject"]] = "TEST EMAIL"
# Paste the body and signatures into the email body
outMail[["HTMLbody"]] = paste0("<p>", body, "</p>", Signature)
# Add your attachment
outMail[["Attachments"]]$Add("C:\\Users\\Some\\Desktop\\file.csv")
outMail$Send()
来源:https://stackoverflow.com/questions/42972222/how-to-send-mails-from-outlook-using-r-rdcomclient-using-latest-version