循环使用特定Gmail标签的邮件(而非线程)

时间:2017-10-10 15:29:46

标签: javascript google-apps-script gmail gmail-api

我使用以下步骤将特定的消息(而不是整个线程)添加到标签to_process

  1. 在Gmail设置中关闭Conversation Mode

  2. 将标签to_process应用于特定邮件

  3. 显示消息时,我可以确认只添加了特定的消息例如,同一个帖子中的另一条消息没有此标签。这很好。

  4. 现在,我想从Google Apps脚本中循环播放所有这些消息。但问题是API只能访问附加到某个标签的线程

    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
    }
    

    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       
    }
    

    如何循环与标签关联的邮件(而非线程)?

    解决方案的开始,但我不知道如何继续:

    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()) {
            //}
    
        }
    }
    

1 个答案:

答案 0 :(得分:0)

我会去 Advanced Gmail ServiceGmail API list(),然后是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's subject is: %s", GmailApp.getMessageById(e.id).getSubject());
  });

}