电子邮件内联图片R.

时间:2015-11-19 22:33:23

标签: r email png

我试图在R中发送带有2个附件的电子邮件,其中一个是我想要内联显示的图片

我可以成功发送带有两个附件的电子邮件,但在内联显示它时会遇到困难。

任何想法都将不胜感激

当前代码:

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>")
         )

2 个答案:

答案 0 :(得分:4)

根据@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)

答案 1 :(得分: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所在的位置。 希望我能引用我从解决方案中得出的所有资料。 祝你好运