【问题标题】:Email inline image R电子邮件内嵌图像 R
【发布时间】:2016-02-22 06:52:02
【问题描述】:

我正在尝试在 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>")
         )

【问题讨论】:

  • 要么使用mailR 包,请查看example。或者对你的图片进行base64编码并将其放入&lt;img src="..."&gt;
  • @lukeA 感谢您的快速回复!两个问题: 1. 如果我使用 mailR 包,它会将我的整个电子邮件转换为 HTML,并且附件必须是 html,而不是 .xlsx。那准确吗? 2. 如果我使用 选项,那仍然是 HTML 吗?

标签: r email png


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    @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 所在的位置。 希望我能引用我从解决这个问题中得出的所有来源。 祝你好运

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-23
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      • 2019-04-30
      相关资源
      最近更新 更多