Sending R Markdown output as body email via outlook (RDCOMclient)

眉间皱痕 提交于 2020-06-24 07:02:50

问题


Just learning R Markdown language and wondering if I can send the output in body email via outlook from R (using RDCOMClient; my office don't use gmail)

Thank you


回答1:


Building upon Ben's answer, the strange symbols showing up everywhere are probably being caused by a enconding mismatch between R and Outlook Object (created by the RDCOMClient Library).

Outlook's basic encoding is "UTF-16", while the basic RMarkdown input is in "UTF-8". To make sure your html output generated by RMarkdown is in "UTF-8" write:

knitr::knit("tale_email_body.Rmd", encoding = "UTF-8")  
eb <- read_lines("tale_email_body.html",locale =  locale(encoding = "UTF-8"))     
Encoding(eb)

You should see a vector whose entries are "UTF-8" or "unknown". Next you have to convert the encoding to the right format using:

eb2 <- paste(eb, sep="", collapse="") 
eb2 <- iconv(eb2, from = "UTF-8",to= "Latin1")

You should them be able to send emails without strange symbols using Ben's code:

library(RDCOMClient)

olMailItem <- 0
OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(olMailItem)

# this retains default Outlook signature
outMail$GetInspector()
signature <- outMail[["HTMLBody"]]

outMail[["To"]] <- sm
outMail[["CC"]] <- paste("egrp",dm,sep=";")
outMail[["subject"]] <- "note this"
outMail[["BodyFormat"]] <- 2
outMail[["HTMLbody"]] <- paste0(eb2, signature)
outMail$Display()
outMail$Send()



回答2:


Short answer:

Create .Rhtml file for email body.

Knit it and read it:

knitr::knit("tale_email_body.Rhtml")  
library("readr", lib.loc="~/R/win-library/3.2")
eb <- read_lines("tale_email_body.html",n_max= -1L)
eb2<-paste(eb, sep="", collapse="") 

Use results in email body:

library(RDCOMClient)

olMailItem <- 0
OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(olMailItem)

# this retains default Outlook signature
outMail$GetInspector()
signature <- outMail[["HTMLBody"]]

outMail[["To"]] <- sm
outMail[["CC"]] <- paste("egrp",dm,sep=";")
outMail[["subject"]] <- "note this"
outMail[["BodyFormat"]] <- 2
outMail[["HTMLbody"]] <- paste0(eb2, signature)
outMail$Display()
outMail$Send()

Let me know if you have questions or improvements.

Partial Credit to: How to add my Outlook email signature to the COM object using RDCOMClient



来源:https://stackoverflow.com/questions/40237343/sending-r-markdown-output-as-body-email-via-outlook-rdcomclient

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