【问题标题】:Sending inline images using mailR使用 mailR 发送内联图像
【发布时间】:2015-01-07 23:39:06
【问题描述】:

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

有什么想法吗?

【问题讨论】:

  • 它可能只是一个过滤器。电子邮件中有这么多“xxx”,Outlook 可能认为它是色情内容,并为了您自己的安全而将其屏蔽。
  • x 只是为了掩盖我在 SO 上的实际电子邮件、密码等——它们实际上并不在我的代码中。
  • 哦,我知道了。不过,仅仅用文字来传达笑话总是比较困难的。
  • 啊,那对不起:)

标签: r email sendmailr


【解决方案1】:

这是一个有效的 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 中提到的工作目录的相对路径,并且 - 当然 - 通过您的数据交换 ...

【讨论】:

  • 酷——是的,这里的诀窍是将图像的本地路径添加为源,而不是 cid。我的最终 HTML 看起来像:.
猜你喜欢
  • 1970-01-01
  • 2016-07-22
  • 2010-11-15
  • 2018-01-20
  • 1970-01-01
  • 1970-01-01
  • 2014-10-02
  • 1970-01-01
相关资源
最近更新 更多