Krby
Krby

Reputation: 125

Email inline image R

I'm trying to send an email within R with 2 attachments, one of which is a picture I'd like to display inline

I can successfully send the email with two attachments, but am stuck in terms of displaying it inline.

Any thoughts would be greatly appreciated

Current code:

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

Upvotes: 5

Views: 2490

Answers (2)

Scottieie
Scottieie

Reputation: 324

@Krby, day late and dollar short, but I was trying to get a sendmailR solution to this problem. Here is what I did:

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

Assumes working directory is where .png is located. Wish I could cite all the sources I drew from the resolve this. Good luck

Upvotes: 0

Krby
Krby

Reputation: 125

Here's final working code, per @lukeA 's suggestion

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)

Upvotes: 6

Related Questions