Search code examples
remailpng

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

Solution

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

    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)