Looping messages (not threads) to a specific Gmail tag

I added the following messages (and not the entire stream) to the label with the to_processfollowing steps:

  • Disable Conversation Modein Gmail settings

  • Apply shortcut to_processto specific messages

  • When displaying messages, I can confirm that only specific messages have been added. For example, another message that is in the same thread has no label . It's good.

Now I would like to use all these messages from Google Apps Script. But the problem is that the API can only provide access to streams attached to a specific label:

var threads = GmailApp.search('label:to_process'); 
for (var i = 0; i < threads.length; i++) {
    // problem: here I cannot access to messages but only threads
}

or

var label = GmailApp.getUserLabelByName("to_process");
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
    // problem: here I cannot access to messages but only threads       
}

How to loop through messages (not threads) associated with a tag?


The beginning of the solution, but I do not know how to proceed:

var threads = GmailApp.search('label:to_process'); 
for (var i = 0; i < threads.length; i++) {
    var messages = threads[i].getMessages();
    for (var j = 0; j < messages.length; j++) {
        var message = messages[j];

        // pseudo code here because getMessageLabels doesn't exist
        //if ("to_process" is in message.getMessageLabels()) {
        //}

    }
}
0
1

Gmail API Gmail(), getMessageById():

function listMessages () {
  // Only return messages matching the specified query.
  var msgs = Gmail.Users.Messages.list('me', {'q':'label:to_process larger:5M'}).messages;

  // For each message - retrieve it by its id
  msgs.forEach(function (e){
    Logger.log("This email subject is: %s", GmailApp.getMessageById(e.id).getSubject());
  });

}
0

All Articles