通过outlook发送R Markdown输出作为正文电子邮件(RDCOMclient)

时间:2016-10-25 10:19:27

标签: r outlook outlook-vba r-markdown rdcomclient

只是学习R Markdown语言并想知道我是否可以通过R来自Outlook的正文电子邮件发送输出(使用RDCOMClient;我的办公室不使用gmail)

谢谢

2 个答案:

答案 0 :(得分:2)

基于Ben的回答,到处显示的奇怪符号很可能是R和Outlook对象之间的不匹配(由RDCOMClient库创建)引起的。

Outlook的基本编码为“ UTF-16”,而基本的RMarkdown输入为“ UTF-8”。为确保RMarkdown生成的html输出在“ UTF-8”中,请输入:

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

您应该看到一个向量,其输入为"UTF-8""unknown"。接下来,您必须使用以下方法将编码转换为正确的格式:

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

您应该使他们能够使用Ben的代码发送没有奇怪符号的电子邮件:

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()

答案 1 :(得分:0)

简短回答:

为电子邮件正文创建.Rhtml文件。

编织并阅读:

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="") 

在电子邮件正文中使用结果:

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()

如果您有任何问题或改进,请告诉我。

部分归功于:How to add my Outlook email signature to the COM object using RDCOMClient