mailR: how to send rmarkdown documents as body in email?

后端 未结 2 544
逝去的感伤
逝去的感伤 2021-01-01 00:03

How to send rmarkdown generated documents as a body in an email, using R?

I have successfully tried knitr with mailR, but when

相关标签:
2条回答
  • 2021-01-01 00:59

    mailR currently does not support resolving inline images encoded using the data URI scheme (http://en.wikipedia.org/wiki/Data_URI_scheme).

    For the time being, I suggest the following solution to address your problem. In the future, I will look into getting mailr to support this natively.

    First off, create the HTML file from the R terminal (the important thing here is that options does not include "base64_images" --- see ?markdown::markdownHTMLOptions):

    library(knitr)
    knit2html("my_report.Rmd",options="")
    

    Now you can send the resulting HTML file via mailR:

    send.mail(from = "FROM@gmail.com",
              to = "TO@gmail.com",
              subject = "MyMail",
              html = T,
              inline = T,
              body = "my_report.html",
              smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "USERNAME", passwd = "PASSWORD", ssl = T),
              authenticate = T,
              send = T)
    
    0 讨论(0)
  • 2021-01-01 01:04

    You can also create the html from R itself. Example here also (sorry for duplication, but formating in the reply to a previous comment was not nicely readable, I reckon)

    A workaround/solution I did was to set the param:

      #------------------
      if (!require(pacman)) install.packages("pacman"); library(pacman)
      p_load("mailR")
      p_load("markdown")
      markdownToHTML("MyReport.Rmd", 
                     output="MyReport.html", 
                     options=c("toc", "use_xhtml", "smartypants", "mathjax", "highlight_code"))
    
      send.mail(from = "myemail@example.com",
                to = c("myemail@example.com", 
                       "myotheremail@example.com"),
                subject = "Email with a Markdown document in HTML at the message body",
                body = "MyReport.html",
                html = TRUE,
                inline = TRUE,
                smtp = list(host.name = "localhost"),
                send = TRUE)
      #------------------
    

    (or choose your own param set for the options of markdownToHTML, while ensuring that you avoid adding the "base64_images")

    This way, I managed to send the html and get the report to show in the body of the email the images included in your report. The images were places in the same folder where the html was generated.

    I hope this helps.

    0 讨论(0)
提交回复
热议问题