电子邮件内联图片 R

5

我正在尝试使用R发送一封带有两个附件的电子邮件,其中一个是我想要嵌入式显示的图片。

我可以成功地发送带有两个附件的电子邮件,但在嵌入式显示方面卡住了。

非常感谢您的任何想法。

当前代码:

setwd(<filepath>)

library(sendmailR)
library(png)


##### SET BASIC EMAIL CHARACTERISTICS

from <- "me@gmail.com"
to <- "them@gmail.com"
subject <- "Sales report"


##### PREPARE ATTACHMENT

# put the body and the mime_part in a list for msg
# x = needs full path if not in working directory
# name = same as attachmentPath if using working directory
attachmentObject <-  mime_part(x="spreadsheet.xlsx",name="spreadsheet.xlsx") 
attachmentObject2 <- mime_part(x="graph.png",name="graph.png")


body <- c("Generic body text", <graph attachmentObject2>)
bodyWithAttachment <- list(body,attachmentObject,attachmentObject2)


##### SEND EMAIL

sendmail(from=from,
         to=to,
         subject=subject,
         msg=bodyWithAttachment,
         control=list(smtpServer="<server name>")
         )

使用 mailR 包,查看示例。或者将您的图片进行 base64 编码,并将其放置在 <img src="..."> 中。 - lukeA
@lukeA 感谢您的快速回复!有两个问题:1. 如果我使用mailR包,它会将我的整个电子邮件转换为HTML格式,并且附件必须是html格式,而不是.xlsx格式。这是否准确?
  1. 如果我使用<img src"...">选项,那么它仍然是HTML格式吗?
- Krby
2个回答

6

这是最终可用的代码,根据 @lukeA 的建议修改过。

library(mailR)
send.mail(from = "me@gmail.com",
          to = "them@gmail.com",
          subject = "Inline image example",
          body = '<p>write text here</p>
                  <img src="R.PNG">
                  <p>more text here</p>',
          html = TRUE,
          inline = TRUE,
          smtp = list(host.name = "<name here>"),
          attach.files=c("R.png", "Project Description.xlsx"),
          authenticate = FALSE)

0

@Krby,虽然有点晚了,但我一直在尝试找到一个sendmailR的解决方案来解决这个问题。以下是我的做法:

#build html body content
managers.msg <- mime_part(paste('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
                                <html xmlns="http://www.w3.org/1999/xhtml">
                                <head>
                                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                                <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
                                <title>Job Coach Billable Hours-Weekly Report</title>
                                <style type="text/css">
                                </style>
                                </head>
                                <body>
                                <p>Hello,</p>
                                <p>Attached is:</p>                              
                                <p><img src="JobCoachBillablePointPlot.png"/></p>
                                </body>
                                </html>', sep=""))

## craft and send the message.
managers.msg[["headers"]][["Content-Type"]] <- "text/html"
from    <- "JobCoachBillable-RPT@domain.org"
to <- list("me@domain.org")
subject <- paste("Job Coach Billable Hours Report, from: ", mindate, " to ", maxdate, sep = "")
attachmentname2 <- "JobCoachBillablePointPlot.png"
attachmentObject2 <- mime_part(x=attachmentname2)
body <- list(managers.msg,attachmentObject2)
html <- TRUE
inline  <- TRUE
mailcontrol   <- list(smtpServer="smtp.domain.org")
sendmail(from=from,to=to,subject=subject,msg=body,control=mailcontrol)

假设工作目录是.png所在的位置。 但愿我能引用所有我从中获得的解决方案的来源。 祝你好运


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接