rgmailmime-typesgmailr

How to send multiple attachments and images(in mail) body using GmailR package?


I have been trying to send 2 images(have to add these in the mail body) and 2 attachments(2 excel files) in one email using the GmailR package but I've not been able to figure it out so far. I've already looked at the syntax from the documentation and this vignette - https://cran.r-project.org/web/packages/gmailr/vignettes/sending_messages.html

Based on the vignette above I've tried a combination of things mentioned in the vignette(attachment and image sections)to add two images in the mail body + two separate xlsx files. It works perfectly fine when I try to send one image/one attachment (as mentioned in the vignetter) but fails to take more than one image/attachment.

#A. # From above mentioned vignette - (This works) - single attachment
email <- gm_mime() %>%
  gm_to('someaddress@somewhere.com') %>%
  gm_from("someaddress@somewhere.com") %>%
  gm_subject("Cars report") %>%
  gm_html_body(
    '<h1>A plot of <b>MotorTrend</b> data <i>(1974)</i></h1>
    <br><img src="cid:foobar">') %>%
  gm_attach_file("mtcars.png", id = "foobar")


#B. (This doesn't work) - more than one attachment? 
email <- gm_mime() %>%
  gm_to('xy@xy.com') %>%
  gm_from("xyz@gmail.com") %>%
  gm_subject(paste("Overview of Etc - ",today_date)) %>%
  gm_html_body(
    '<h1>Total Nos<b>XYZ</b> Region <i>(Year)</i></h1>
    <br><img src="cid:foobar"><img src="cid:foobar2">') %>%
  gm_attach_file(c("Overview1.jpeg","Overview2.jpeg"
,"file1.xlsx","file2.xlsx"), id = c("foobar1","foobar2"))

Are there any workarounds that anyone can suggest? Would be grateful if people can point me in the right direction!


Solution

  • Turns out, we just had to pipe in the subcommands again!

    email <- gm_mime() %>%
      gm_to('xy@xy.com') %>%
      gm_from("xyz@gmail.com") %>%
      gm_subject(paste("Overview of Etc - ",today_date)) %>%
      gm_html_body(
        '<h1>Total Nos<b>XYZ</b> Region <i>(Year)</i></h1>
        <br><img src="cid:foobar"><img src="cid:foobar2">') %>%
      gm_attach_file("Overview1.jpeg", id = c"foobar1") %>%
      gm_attach_file("Overview2.jpeg", id = c"foobar2")%>%
      gm_attach_file("attach1.xlsx")%>%
      gm_attach_file("attach2.xlsx")
    
    gm_send_message(email)