使用 mailR 发送内联图像

Sending inline images using mailR

我正在尝试将 R 创建的 JPEG 图像嵌入到电子邮件中,目的是创建一封自动显示每日图表的动态文本电子邮件。我能够附上图片,并指定竞争 ID;但是,当我发送消息并在 Outlook 中打开结果时,我在图像应该出现的位置出现了一个问号。该图像确实成功地附加到电子邮件中,看起来图像只是没有在 HTML 中内嵌渲染。

这是我的示例代码:

library(mailR)

send.mail(from = "xx@xxx.com",
          to = "xxx@xxx.com",
          subject = paste("Results for the date ending ", Sys.Date()-1, sep = ""),
          body = '<html> Test image - <img src="cid:test_img.jpg" /></html>',
          html = TRUE,
          smtp = list(host.name = "xxx.xxx.com", user.name = "xxx@xxx.com", passwd = "xxx"),
          attach.files = '/Users/xxx/Documents/Rplots/test_img.jpg',
          authenticate = TRUE,
          inline = TRUE,
          send = TRUE)

对正在发生的事情有什么想法吗?

这是一个有效的 Gmail 示例:

library(mailR)
png(file.path(getwd(), "..", "img.png")); plot(0); dev.off()
# Gmail users may have to switch https://www.google.com/settings/security/lesssecureapps before the send
send.mail(from = "...@gmail.com",
          to = "...@gmail.com",
          subject = "Subject of the email",
          body = '<img src="../img.png">',
          html = TRUE,
          inline = TRUE,
          smtp = list(host.name = "smtp.gmail.com", 
                      port = 465, 
                      user.name = "...", 
                      passwd = "...", 
                      ssl = TRUE),
          authenticate = TRUE,
          send = TRUE)

您必须参考提到的工作目录的相对路径 here,并且 - 当然 - 用您的数据交换 ...