2

I am trying to embed a JPEG image created by R into an e-mail, with the intent of creating an automated daily e-mail that displays a chart with dynamic text. I was able to attach the image, and designating the contend ID; however, when I send the message and open the result in Outlook, I get a question mark where the image should be. The image does indeed attach to the e-mail successfully, it looks like the image just isn't rendering inline in the HTML.

Here's my sample code:

library(mailR)

send.mail(from = "xx@xxx.com",
          to = "xxx@xxx.com",
          subject = paste("Results for the date ending ", Sys.Date()-1, sep = ""),
          body = '<html> Test image - <img src="cid:test_img.jpg" /></html>',
          html = TRUE,
          smtp = list(host.name = "xxx.xxx.com", user.name = "xxx@xxx.com", passwd = "xxx"),
          attach.files = '/Users/xxx/Documents/Rplots/test_img.jpg',
          authenticate = TRUE,
          inline = TRUE,
          send = TRUE)

Any ideas on what's going on?

Bryan
  • 5,999
  • 9
  • 29
  • 50

1 Answers1

3

Here is a working Gmail example:

library(mailR)
png(file.path(getwd(), "..", "img.png")); plot(0); dev.off()
# Gmail users may have to switch https://www.google.com/settings/security/lesssecureapps before the send
send.mail(from = "...@gmail.com",
          to = "...@gmail.com",
          subject = "Subject of the email",
          body = '<img src="../img.png">',
          html = TRUE,
          inline = TRUE,
          smtp = list(host.name = "smtp.gmail.com", 
                      port = 465, 
                      user.name = "...", 
                      passwd = "...", 
                      ssl = TRUE),
          authenticate = TRUE,
          send = TRUE)

You have to reference the relative path to the working directory as mentioned here, and - of course - exchange ... by your data.

lukeA
  • 53,097
  • 5
  • 97
  • 100
  • 1
    Cool -- yeah, the trick here is to add the local path to the image as the source, not the cid. My final HTML looks like: . – Bryan Jan 08 '15 at 14:15