sky_megh
sky_megh

Reputation: 155

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('[email protected]') %>%
  gm_from("[email protected]") %>%
  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('[email protected]') %>%
  gm_from("[email protected]") %>%
  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!

Upvotes: 3

Views: 808

Answers (2)

Ljupcho Naumov
Ljupcho Naumov

Reputation: 465

A more elegant way to do it with a variable number of attachments.

# The attach_multiple function is used by reduce. 
# Reduce works by taking the result of the function and using it as the first argument in the next iteration.
# In effect you can replace a repetitive chain of pipe commands.

attach_multiple <- function(mime, attachment, ...) {
  mime %>% 
    gmailr::gm_attach_file(attachment, ...)
}

attachments <- c("Overview1.jpeg", "Overview2.jpeg", "attach1.xlsx", "attach2.xlsx")


email <- gm_mime() %>%
  gm_to('[email protected]') %>%
  gm_from("[email protected]") %>%
  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">') %>%
  purrr::reduce(.init = ., .x = attachments, .f = attach_multiple)

gm_send_message(email)

Upvotes: 1

sky_megh
sky_megh

Reputation: 155

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

email <- gm_mime() %>%
  gm_to('[email protected]') %>%
  gm_from("[email protected]") %>%
  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)

Upvotes: 1

Related Questions