r - 如何在 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 文件位于同一文件夹中。谢谢你。
解决方案
我有同样的问题,我终于明白了。使用你的代码应该是这样的:
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 引用图像
推荐阅读
- python - 在 python 中计算重复项。为什么我们应该只取 set 而不是 list 或 dict?
- bash - 让 gitlab 工作不等待“脚本”部分的任何返回
- python - 在数据python中用nans计算3个数组的加权平均值
- javascript - 方法测量模拟期间代理未使用的时间
- c# - 我想在 C# 表单应用程序的列表框中打印 C++ 向量的元素
- python - 如何在 Paramiko 库中配置首选密码
- arrays - 在c中访问动态数组的每个元素
- vbscript - 使用 VBScript 递归检查复制项目到新目标的文件和文件夹
- python - 从异常对象中提取完整跟踪
- c++ - 如何使用 CMake 将 Go 代码编译并链接到 c++ 中