Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

I have successfully tried knitr with mailR, but when instead generating the html-report with the (new) rmarkdown-package it fails.

library(mailR)
send.mail(
  from = "[email protected]",
  to = "[email protected]",
  subject = "MyMail",
  html = T,
  inline = T,
  body = "my_report.html",
  smtp = list(host.name = "smtp.gmail.com", port = 465,
    user.name = "USERNAME", passed = "PASSWORD", ssl = T),
  authenticate = T,
  send = T
)

error:

org.apache.commons.mail.EmailException: Building the MimeMessage failed
    at org.apache.commons.mail.ImageHtmlEmail.buildMimeMessage(ImageHtmlEmail.java:110)
    at org.apache.commons.mail.Email.send(Email.java:1436)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at RJavaTools.invokeMethod(RJavaTools.java:386)
Caused by: java.io.IOException: Cant resolve the following file resource :/Users/USERNAME/myfolder/./data:image/png;base64,iVBORw0KGg …

(…)

… SuQmCC
    at org.apache.commons.mail.resolver.DataSourceFileResolver.resolve(DataSourceFileResolver.java:105)
    at org.apache.commons.mail.resolver.DataSourceFileResolver.resolve(DataSourceFileResolver.java:79)
    at org.apache.commons.mail.ImageHtmlEmail.replacePattern(ImageHtmlEmail.java:149)
    at org.apache.commons.mail.ImageHtmlEmail.buildMimeMessage(ImageHtmlEmail.java:103)
    ... 6 more
Error: EmailException (Java): Building the MimeMessage failed

I guess it has to do with the following line: Cant resolve the following file resource :/Users/USERNAME/myfolder/./data:image/png;base64?

I'm more than grateful for any kind of guidance.

like image 956
reinholdsson Avatar asked Jun 21 '14 23:06

reinholdsson


People also ask

How do I convert RMarkdown to HTML?

To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want. When you click the button, rmarkdown will duplicate your text in the new file format.


1 Answers

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 = "[email protected]",
          to = "[email protected]",
          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)
like image 84
Rahul Premraj Avatar answered Sep 18 '22 13:09

Rahul Premraj