我有一个代码可以使用 sendmailR 在电子邮件正文中发送内嵌图像。但是收到电子邮件后,其中不包含图像。它显示错误:无法显示链接的图像。文件可能已被移动、重命名或删除。
下面是代码
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 文件位于同一文件夹中。 谢谢。
最佳答案
我也有同样的疑问,终于明白了。使用您的代码应该类似于:
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 引用图像
关于r - 如何在 sendmailR 电子邮件中嵌入内联图像(不是附件)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62012061/