Send multiple personalized emails using gmailr from R

27 views Asked by At

I'm using gmailr for work. I've been asked to send a mail to multiple companies requesting their assistance to a study. For that I'm trying to use the gmailr and glue packages fundamentally.

Here is the code:

send <- c("[email protected]","[email protected]")

names <- c('xxx','yyy')

from <- "[email protected]"

text <- glue("Hi {names}, \n email test")

files <- c("xxx_invitation_letter.pdf","yyy_invitation_letter.pdf")

for (i in names) {
  
  gm_mime() %>% 
    gm_to(send[i]) %>% 
    gm_from(from) %>% 
    gm_subject(paste("Inv. to study")) %>% 
    gm_text_body(glue({text[i]},"no: {[i]}")) %>%
    gm_attach_file(filename = files[i]) %>% 
    gm_send_message()
}

The error I get is: "Auto-refreshing stale OAuth token. Error in gmailr_POST(c("messages", "send"), user_id, class = "gmail_message", : Gmail API error: 400 Invalid To header"

Perhaps to do this I should try another code or something. The idea is to send personalized emails to all those receivers.

Would appreciate any help.

0

There are 0 answers