GMailApp Gmail search

2020-03-30 02:10发布

问题:

I have a problem with the GmailApp search(query) function.

According to the manual it uses Gmail queries, so all the arguments should be accessible and return correct results.

This is my query:

var sent_threads = GmailApp.search('in:sent after:2016/02/29');

  Logger.log(sent_threads.length); // This one prints 21

  for (var i = 0; i < sent_threads.length; i++) {

    var message = sent_threads[i].getMessages()[0];
    var recipient = message.getTo();
    Logger.log(recipient); // This one prints 21 email recipients

  }

But when I go to the Gmail.com and type in the query in search bar I will get 44 messages. Any idea why those results are different?

回答1:

It's because the threads are collections of messages.

Try looping through the result of getMessages() instead of just accessing element [0].