如何使用R将rmarkdown
生成的文档作为正文作为电子邮件发送?
我已经成功地用knitr
尝试了mailR
,但是当用(新)rmarkdown
-package生成html-report时,它失败了。
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
)
错误:
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?
我非常感谢您提供任何指导。
最佳答案
mailR当前不支持解析使用数据URI方案(http://en.wikipedia.org/wiki/Data_URI_scheme)编码的嵌入式图像。
目前,我建议采用以下解决方案来解决您的问题。将来,我将研究使mailr本身支持它。
首先,从R终端创建HTML文件(这里重要的是,选项不包括“ base64_images
” ---参见?markdown::markdownHTMLOptions
):
library(knitr)
knit2html("my_report.Rmd",options="")
现在,您可以通过mailR发送结果HTML文件:
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)