Search code examples
rflextable

Customising the flextable and attaching files for Emails


In relation to my previous question,Send an email with dataframe as a flextable i want to add a few bits to that question.

Here is the dataset:

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

And here is the code,

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

I want to send a dataframe as a flextable as the body of this email, along with an email attachment. But right now, i am able to send across the email attachment. There is no body coming across in the email.

Any help would be appreciated. thanks.


Solution

  • In your code, you made a mistake, you wrote

    body    <- list(msgJP)
    # ...
    bodyWithAttachment <- list(body,attachmentObject)
    

    argument msg in function sendmail is expecting a list of MIME values, not a list of list, so you should write instead :

    bodyWithAttachment <- list(msgJP,attachmentObject)