2015-11-19 114 views
3

我試圖R內發送一封電子郵件,2個附件,其中之一是一個圖片,我想顯示嵌入式電子郵件內嵌圖像R

我可以成功地與兩個附件發送電子郵件,但我卡在內嵌顯示中。

任何想法,將不勝感激

當前代碼:

setwd(<filepath>) 

library(sendmailR) 
library(png) 


##### SET BASIC EMAIL CHARACTERISTICS 

from <- "[email protected]" 
to <- "[email protected]" 
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>") 
     ) 
+0

或者使用'mailR'包代替 - 看看[示例](https://github.com/rpremraj/mailR)。或者對你的圖像進行base64編碼,並將其放在''內。 – lukeA

+0

@lukeA感謝您的快速回復!兩個問題:1.如果我使用mailR軟件包,它會將我的整個電子郵件轉換爲HTML,並且附件必須以html格式,而不是.xlsx格式。這是否準確? 2.如果我使用選項,那仍然是HTML嗎? – Krby

+0

1.不是和2.是。 – lukeA

回答

3

這裏是最後的工作代碼,每@lukeA的建議

library(mailR) 
send.mail(from = "[email protected]", 
      to = "[email protected]", 
      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 <- "[email protected]" 
to <- list("[email protected]") 
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所在的位置。 希望我可以引用我從這個解決方案中提取的所有資源。 祝你好運

相關問題