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.
But when I change the code to use the flextable function, all I am sending is a blank mail with no body at all.
This is the sample data set
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")
Please find the code below.
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.
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.
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>')))
来源:https://stackoverflow.com/questions/54841507/send-an-email-with-dataframe-as-a-flextable