使用mailR发送内嵌图像

时间:2015-01-07 23:39:06

标签: r email sendmailr

我正在尝试将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)

有关正在发生的事情的任何想法?

1 个答案:

答案 0 :(得分:3)

以下是一个有效的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,当然 - 您的数据会交换...