首页 > 解决方案 > 如何在 sendmailR 电子邮件中嵌入图像内联(不是附件)?

问题描述

我有一个代码可以使用 sendmailR 在电子邮件正文中内联发送图像。但是,在收到电子邮件后,它不包含图像。它显示错误:The linked image cannot be displayed.The file may have been moved,renamed or deleted. 下面是代码

library(sendmailR)
image<-"image.png"
body<-sendmailR::mime_part("<html><p>This is a picture.</p> 
<img src='image.png' >
<p> Image is:</p>")
body[["headers"]][["Content-Type"]] <- "text/html"

 sender <- "sender email"
  recipients <- "receiver email"
  subject <- "Test Email"

 sendmailR:: sendmail(sender, recipients, subject, list(body),control=list(smtpServer="server address"))

该图像与 rmd 文件位于同一文件夹中。谢谢你。

标签: rmimesendmailr

解决方案


我有同样的问题,我终于明白了。使用你的代码应该是这样的:

library(sendmailR)
attachmentPath <-paste(getwd(),"/image.png",sep="")
attachmentObject <-mime_part(x=attachmentPath,name="plot.png")
msg <- mime_part('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
 
  <style type="text/css">
  </style>
</head>
<body>
<h1>HTML email</h1>

<img src="cid:plot.png" >

</body>
</html>')
msg[["headers"]][["Content-Type"]] <- "text/html"
body    <- list(msg,attachmentObject)

 sender <- "sender email"
  recipients <- "receiver email"
  subject <- "Test Email"

 sendmailR:: sendmail(sender, recipients, subject, body,control=list(smtpServer="server address"))

如您所见,您必须使用 cid 引用图像


推荐阅读