Assign a label to Gmail emails from a dataset using {gmailr}

34 Views Asked by At
library(tidyverse)
library(gmailr)

I'm trying to assign a label to a specific set of emails using the {gmailr} package.

This is necessary in order to download specific emails/attachments. So far, I have only been able to create the label, but I can't assign it to any email via {gmailr}.

Here are my procedures:

  1. With a properly created app in the Google Cloud Console, I obtain the credentials to access my Gmail. From there, I use the functions gm_auth_configure() and gm_auth() to authorize my R session to access Gmail.

  2. With the label created manually, I can access the email IDs.

  3. Assuming I have created a label called 'teste':

emails <- gm_messages(search = 'label:teste', user_id = 'me')
  1. I then obtain a list with "message_id" and "thread_id".

  2. With the gm_id() function, I can extract the IDs of each email:

emails_id <- emails %>%
  gm_id()

  1. From there, I use the map() function from {purrr} to extract the information I need from each email:
emails_id %>%
   map(~{
   email_data <- gm_message(.x)
   email_from <- gm_from(email_data)
   email_attach <- gm_attachments(email_data)
})

However, I only achieved my goal because initially I manually assigned the "teste" label to each of the emails that interested me. What I would like to know is how to make this assignment via {gmailr} using a set of names or emails.

The most I can do is create the label using gm_create_label('teste'), but I can't assign it to any sender.

For example, consider a dataset:

df <- tibble(nome = c('FULANO', 'BELTRANO', 'CICLANO'),
             email = c('[email protected]', '[email protected]', '[email protected]'))


nome     email             
  <chr>    <chr>             
1 FULANO   [email protected]  
2 BELTRANO [email protected]
3 CICLANO  [email protected] 

How can I assign the 'teste' label to each of the names or emails in the dataset?

0

There are 0 best solutions below