刚刚学习R Markdown语言,想知道我是否可以通过outlook从R发送正文电子邮件中的输出(使用RDCOMClient;我的办公室不使用gmail)
谢谢
根据Ben的回答,到处出现的奇怪符号可能是由R和Outlook Object(由RDCOMClient Library创建)之间的编码不匹配引起的。
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()
简短回答:
为邮件正文创建。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()
如果你有问题或改进,请告诉我。
部分归功于:如何使用RDCOMClient将我的Outlook电子邮件签名添加到COM对象