Home > Mobile >  How can I perform a function for every email with a specific label?
How can I perform a function for every email with a specific label?

Time:08-18

I have daily reports sent to me by email with the label "Automated Reports". I have a function that i would like to run for every email with that label, so i wrote the following code to get all emails with that label.

function processReportEmails() {
  var label = GmailApp.getUserLabelByName("Automated Reports");
  var threads = label.getThreads(); 
  var messages = threads[threads.length - 1].getMessages(); 
  
  for (var i = threads.length - 1; i >= 0; i--){
     var msg = messages[messages.length - 1];

/perform my function with the email here using msg

    threads[threads.length - 1].removeLabel(label).refresh();

  }
}

the issue is, it ends up performing the custom function on the same email for the amount of emails there are with that label. how can I make this work for each email, eventually ending up with no more items with that label?

(i am kind of new to this stuff, i assume its a simple syntax answer. also while searching for an answer there were many questions with the same goal as mine, but none solving this specific issue)

CodePudding user response:

I agree with @Rubén answer, that change will remove the label from all the messages.

However, I'll like to provide a different approach. I reviewed this code with a Friend (@Juan Serrano JKL ), and this sample code will fetch all the messages inside with those labels, not just the first one, and also will delete the label from the messages that were already fetched. You can change the code as need it.

Here is the sample code:

function iterateEmails() {
  const labelName = 'testing';

  var label = GmailApp.getUserLabelByName(labelName);
  
  var threads = label.getThreads();

  // This logs the amount of threads with that label, 

  console.log("There are "   threads.length   " threads in total with the label: "   labelName);
  
  // This complete a loop to get all the messages inside those threads. 
  threads.forEach( function(thread){
    var messages = thread.getMessages();

    // This logs provide you information on how many emails are in each thread. 
    // and prints the number of messages. 

    console.log("The threadId: '"   thread.getId()   "' contains "   messages.length   " messages");
    console.log("Printing found messages:");

    messages.forEach( function(message){
      // Currently, it exports the information to the console.
      // However, you can replace this with what you want to do with the message once it's fetched. 
      var messageBody = message.getPlainBody().substring(0,50);
      var messageTitle= message.getSubject();

      // prints in the console the subject, title and the body of the messages. 

      console.log("Title: %s\n\tBody: %s", messageTitle, messageBody);
    });

    // And the last one removes the the labels of the information that was exported. 

    console.log("Removing the label from the threadId: '"   thread.getId());
    thread.removeLabel(label);
  });
}

**Reference: **

CodePudding user response:

Replace

threads[threads.length - 1].removeLabel(label).refresh();

by

threads[i].removeLabel(label).refresh();
  • Related