发送带有数据框作为灵活表的电子邮件 [英] Send an email with dataframe as a flextable

查看:126
本文介绍了发送带有数据框作为灵活表的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于每天发送邮件的代码,并且使用xtable包来完成它. 但是最近,我已经习惯于使用flex表,因为它对我的要求更可行.

I have a code which I use to send mails everyday and I use the xtable package to get it done. But lately, I've got accustomed to using the flex table as it is more feasible to my requirements.

但是当我更改代码以使用flextable函数时,我所发送的只是一封没有任何正文的空白邮件.

But when I change the code to use the flextable function, all I am sending is a blank mail with no body at all.

这是示例数据集

 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)



msgJP <- try(mime_part(paste('<!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>",print(flextable(samplemon),include.rownames = FALSE, type = 'html'),'</body>
                           </html>')))


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

body    <- list(msgJP)

from <- "abc@xyz.com"
to<-c("abc@xyz.com")
subject <- paste0("Why is this not working?")
sendmail(from, to, subject, body, control = list(smtpServer="smtp.amazon.com"))

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

Any Help would be appreciated. Thanks.

推荐答案

这是因为print方法显示的是flextable,它不返回HTML值.方法format(fletable_obj, type = "HTML")返回HTML值.

This is because print method is displaying the flextable, it does not return the HTML value. The method format(fletable_obj, type = "HTML") return the HTML value.

您应将HTML创建修改为:

You should modify the HTML creation as:

msgJP <- try(mime_part(paste('<!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(flextable(samplemondata), type = "html"),
'</body>
</html>')))

这篇关于发送带有数据框作为灵活表的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆