mailR icon indicating copy to clipboard operation
mailR copied to clipboard

Sending RMarkdown v2 reports in the body of an email

Open the-tourist- opened this issue 9 years ago • 4 comments

Is it possible to send RMarkdown v2 reports in the body of an email. Your work-around will work for RMarkdown v1, but knit2html seems to only render v1 Markdown, meaning that some of the useful new features of v2, especially the fine control over css styling don't work. I have also raised this question on StackOverflow, but so far have not got any useful replies.

the-tourist- avatar Sep 16 '15 16:09 the-tourist-

I'm also very interested in sending Markdown v2 documents through email. Is there a work around to change the HTML (perhaps after it has been rendered) to get it to not have base64 encoding and send as the email body?

mgiglia avatar Sep 28 '16 13:09 mgiglia

Hi,

I ended up sending it using Markdown v1. In order to format using css I just coded

and other html directly into the rmarkdown document. I have included the code I use to do the pre and post processing in order to send the document both in the body of the email and as an attachment.

One other thing I would note, since it took me quite a lot of trial and error before I found this out. Email clients are quite patchy and inconsistent in their handling of css. In order to position things correctly, tables need to be used rather than css (very 1999).

Anyway, I hope this helps.

Graeme

Header in rmarkdown:

output: html_document: self_contained: false theme: null css: TheCSS.css

R:

require(rmarkdown) require(mailR) require(knitr) require(juicer) # https://github.com/Automattic/juice

ReportName <- "ReportName"

Create a second version of the report with self_contained = true in order

to attach that to the email RmdReport <- readLines(Conn <- file(paste0(ReportName, ".Rmd"))) close(Conn) SCReport <- gsub("self_contained: false", "self_contained: true", RmdReport, fixed = T) write(SCReport, paste0(ReportName, "-attachment.Rmd")) render(paste0(ReportName, "-attachment.Rmd"))

Render in rmarkdown v1

render(paste0(ReportName, ".Rmd"))

Remove any js scripts from the html

HtmlReport <- readLines(Conn <- file(paste0(ReportName, ".html"))) close(Conn) HtmlReport <- HtmlReport[-grep("", HtmlReport)] write(HtmlReport, paste0(ReportName, "Temp.html"))

Inline all the css

write(juice(paste0(ReportName, "Temp.html"), css = "Temp.css", options=list(applyStyleTags=TRUE, removeStyleTags=FALSE)), paste0(ReportName, "Processed.html")) file.remove(paste0(ReportName, "Temp.html"))

Send Mail

send.mail(from = "[email protected]", to = "[email protected]", subject = "Subject", html = TRUE, inline = TRUE, body = paste0(ReportName, "Processed.html"), #body = "Test", attach.files = paste0(ReportName, "-attachment.html"), smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "User", passwd = "Password", ssl = TRUE), authenticate = TRUE, send = TRUE)

On Wed, Sep 28, 2016 at 3:46 PM, M.Giglia [email protected] wrote:

I'm also very interested in sending Markdown v2 documents through email. Is there a work around to change the HTML (perhaps after it has been rendered) to get it to not have base64 encoding and send as the email body?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/rpremraj/mailR/issues/32#issuecomment-250171010, or mute the thread https://github.com/notifications/unsubscribe-auth/AG41NClP4NpOlFklxI6umbgHo6AmbefBks5qum_LgaJpZM4F-iGq .

the-tourist- avatar Sep 29 '16 10:09 the-tourist-

Is this supposed to work ? I can't find this juicer package anywhere ...

statquant avatar Oct 20 '17 21:10 statquant

It should, although I removed a quite a few lines from the that were specific to my needs and edited things a bit. So there could be errors in the code.

I think the link to the juicer package is https://github.com/noamross/juicer

And sorry about the formatting of the previous post. I replied by email, so everything got converted to markdown, including css, and comments (# became heading1).

On Fri, Oct 20, 2017 at 11:25 PM, statquant [email protected] wrote:

Is this supposed to work ? I can't find this juicer package anywhere ...

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/rpremraj/mailR/issues/32#issuecomment-338326024, or mute the thread https://github.com/notifications/unsubscribe-auth/AG41NKyDRCFoeHmoV5ZACvJsGWagP69tks5suQ-9gaJpZM4F-iGq .

the-tourist- avatar Oct 20 '17 22:10 the-tourist-