自定义电子邮件的弹性表和附件文件

时间:2019-02-23 19:31:05

标签: r flextable

关于我之前的问题,Send an email with dataframe as a flextable我想在这个问题上添加一些内容。

这是数据集:

samplemondata<-structure(list(Root.Cause = c("Blocking", "Created in Error", 
"Duplicate", "Horizontal liquid bottle", "Overhanging", "Title Not Facing  Out", 
"Trash in the Bin", "Units Not Stowed Securely", "Unorganized", 
"Wrong Bin Type"), BCN1 = c("109", "", "", "", "", "70", "", 
"7", "1", "6"), FCO1 = c("98", "1", "", "1", "", "31", "4", "4", 
"", "4"), FRA7 = c("401", "", "", "", "2", "260", "", "2", "", 
"100"), HAM2 = c("414", "", "", "", "1", "115", "", "1", "1", 
"44"), LCY2 = c("230", "", "", "1", "1", "102", "", "3", "", 
"15"), LTN4 = c("30", "", "", "", "", "7", "", "", "", ""), MAN1 = c("66", 
"", "", "", "1", "22", "3", "1", "", "3"), MAN2 = c("104", "", 
"", "", "", "50", "", "2", "", "12"), MAN3 = c("92", "", "", 
"1", "", "36", "", "1", "", "5"), SZZ1 = c("344", "", "", "", 
"2", "114", "1", "15", "", "10")), row.names = c(NA, -10L), class = "data.frame")

这是代码,

library(sendmailR)
library(dplyr)
library(flextable)


ft<-flextable(samplemondata)
ft<-theme_vanilla(ft)
ft<-bg(ft,bg="black",part="header")
ft<-color(ft,color="white",part="header")
ft <- color(ft,  i = ~ samplemondata$BCN1>50 , color = "orange")




msgJP <- try(mime_part(paste0('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
                           Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
                           <html xmlns="http://www.w3.org/1999/xhtml">
                           <head>
                           <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                           <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
                           <title>HTML demo</title>
                           <style type="text/css">
                           </style>
                           </head>  
                           <body>',"hello,<br>","check out the data for self audit.","<br>",format(ft,type='html'),'</body>
                           </html>')))


 msgJP[["headers"]][["Content-Type"]] <- "text/html"

 body    <- list(msgJP)
 attachmentPath <- "filepath/name.csv"
 attachmentName<- "myname.csv"

 attachmentObject <- mime_part(x=attachmentPath ,name=attachmentName)
 bodyWithAttachment <- list(body,attachmentObject)

from <- "abc@xyz.com"
to<-c("abc@xyz.com")
subject <- paste0("Let this work")

sendmail(from, to, subject, bodyWithAttachment , control = list  (smtpServer="smtp.amazon.com"))

我想发送一个数据框作为此电子邮件的主体作为可伸缩表格,以及电子邮件附件。但是现在,我可以通过电子邮件附件发送邮件了。电子邮件中没有正文。

任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:0)

在您的代码中,您犯了一个错误,您编写了

body    <- list(msgJP)
# ...
bodyWithAttachment <- list(body,attachmentObject)
函数sendmail中的

自变量msg需要一个MIME值列表,而不是一个列表列表,因此您应该编写:

bodyWithAttachment <- list(msgJP,attachmentObject)