remailsendmailr

Sending inline images using mailR


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?


Solution

  • 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.