mailR:如何在电子邮件中将rmarkdown文件作为正文发送?

时间:2014-06-21 23:44:19

标签: r email r-markdown sendmailr

如何使用R?

rmarkdown生成的文档作为正文发送到电子邮件中

我已成功使用knitr尝试了mailR,但是当使用(新)rmarkdown - 包生成html-report时,它失败了。

library(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", passed = "PASSWORD", ssl = T),
  authenticate = T,
  send = T
)

错误:

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

我想这与以下行有关:无法解析以下文件资源:/ Users / USERNAME / myfolder /./ data:image / png; base64

我非常感谢任何指导。

2 个答案:

答案 0 :(得分:10)

mailR目前不支持解析使用数据URI方案(http://en.wikipedia.org/wiki/Data_URI_scheme)编码的内嵌图像。

目前,我建议采用以下解决方案来解决您的问题。将来,我会考虑让mailr本地支持这个。

首先,从R终端创建HTML文件(这里重要的是选项不包括&#34; base64_images&#34; ---请参阅?markdown::markdownHTMLOptions):< / p>

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

现在您可以通过mailR:

发送生成的HTML文件
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)

答案 1 :(得分:0)

您还可以从R本身创建html。这里也有示例(很抱歉重复,但是我认为对先前评论的回复格式不好读)

我所做的一种解决方法/解决方案是设置参数:

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

(或为markdownToHTML选项选择自己的参数集,同时确保避免添加“ base64_images”)

这样,我设法发送了html并获得了报告,以在电子邮件的正文中显示报告中包含的图像。这些图像位于生成html的同一文件夹中。

我希望这会有所帮助。