1

I have multiple lists which look like this:

list(structure(list(mail = c("[email protected]", "[email protected]", "[email protected]", 
"[email protected]", "[email protected]", "[email protected]", "[email protected]"), file = c("file1.pdf", 
"file2.pdf", "file3.pdf", "file4.pdf", "file5.pdf", "file6.pdf", 
"file7.pdf")), row.names = c(NA, 7L), class = "data.frame"))

As you can see, it's the same email but with multiples files associated to it. So what I want is to attach all this files at once when I create a draft considering that for every email, the number of files won't be the same. Since that can't be done directly with gm_attach_file() i'm trying to use purrr::reduce(). When I execute:

files_vector <- data.frame(arc= test[[1]][[2]])
purrr::reduce(.x = files_vector, .f = gm_attach_file)

I get all the files I want to send:

[1] "file1.pdf" "file2.pdf" "file3.pdf" "file4.pdf" "file5.pdf" "file6.pdf" "file7.pdf"

I must say that:

files_vector <- data.frame(arc= test[[1]][[2]])

doesn't work with my original data since I got this error:

Error in mime$parts : $ operator is invalid for atomic vectors

When I run the whole code for creating an email, I just got an empty draft with no recipient and no attached files at all.

gm_mime()|>
    gm_to(sprintf("%s", unique(test[[1]][[1]])))|>
    gm_from("[email protected]") |>
    gm_subject("The files you requested")|>
gm_html_body(body = paste(glue::glue(
    "<h5><b> <b></h5>
    <p>
    <p> </p>
    <p> .</p>"

)))|>
    purrr::reduce(.x = files_vector, .f = gm_attach_file)|>
    gm_create_draft()

I don't know if I'm doing something wrong or it's simply that gmailr is somewhat limited on how many files you could attach to an email. Any feedback will be much appreciated.

I must add that I also reviewed this: How to send multiple attachments and images(in mail) body using GmailR package? and the solution it's given there doesn't work at all.

1 Answer 1

1

I don't have credentials setup so I can't fully test this, but I think this should work.

library(gmailr)

test <- list(structure(list(mail = c("[email protected]", "[email protected]", "[email protected]", 
                                     "[email protected]", "[email protected]", "[email protected]", "[email protected]"), 
                            file = c("file1.pdf", 
                                     "file2.pdf", "file3.pdf", "file4.pdf", "file5.pdf", "file6.pdf", 
                                     "file7.pdf")), 
                            row.names = c(NA, 7L), class = "data.frame"))


prepare_email <- function(mail, files){
    base_email <- gm_mime()|>
        gm_to(mail) |>
        gm_from("[email protected]") |>
        gm_subject("The files you requested")|>
        gm_html_body(body = paste(glue::glue(
            "<h5><b> <b></h5>
    <p>
    <p> </p>
    <p> .</p>"
            
        )))
    
    purrr::reduce(.x = files, .f = gm_attach_file, .init = base_email)
}

ready_to_send <- test[[1]] |>
    dplyr::as_tibble() |> 
    dplyr::summarise(.by = mail, files = list(file)) |> 
    purrr::pmap(prepare_email)
    

I use dplyr to create a pair of lists (columns of the tibble) and use purrr to map through them. The prepare_email function first prepares the base email without the attachments. Then I use purrr::reduce to attach the files. Specify the base email with the .init parameter.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.