How to send mails from outlook using R RDCOMClient using latest Version?

心不动则不痛 提交于 2020-01-05 07:56:21

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!