如何过滤掉来自 Gmail 中邮寄列表的所有电子邮件

How to filter out all emails that came from a mailing list in Gmail

有没有办法使用搜索查询过滤掉来自 Gmail 或 Google Apps 脚本中邮寄列表的所有电子邮件。我知道您可以使用 list:info@example.com 过滤掉特定的电子邮件地址。但我想要一个包罗万象的查询类型,甚至是一个包罗万象的来自特定域的查询,例如 list:@example.com。但是,这不起作用。有任何想法吗?非常感谢任何帮助,谢谢!

此功能将删除所有收件箱线程中不在列表中的所有邮件。

function emailFilter() {
  var list=['a@company.com','b@company.com','c@company.com','d@company.com','e@company.com'];
  var threads=GmailApp.getInboxThreads();
  var token=null;
  for(var i=0;i<threads.length;i++) {
    if(threads[i].getMessageCount()) {
      var messages=threads[i].getMessages();
      for(var j=0;j<messages.length;j++) {
        if(list.indexOf(messages[j].getFrom()==-1)) {
          messages[j].moveToTrash();
        }
      }
    }
  }
}

我没有测试过,因为我的收件箱一直都是空的。您可能希望将 'moveToTrash()' 替换为 'star()' 以进行测试

我从你的问题和评论中了解到,你需要过滤用户收件箱中他收到的电子邮件,这些电子邮件不仅包含某个标签,还包含某个域。如果我理解得很好,这段代码可以帮助你:

function checkLabels() {
  // Get the threads from the label you want
  var label = GmailApp.getUserLabelByName("Label Test List");
  var threadArr = label.getThreads();
  // Init variable for later use
  var emailDomain = '';
  // Iterate over all the threads
  for (var i = 0; i < threadArr.length; i++) {
    // for each message in a thread, do something
    threadArr[i].getMessages().forEach(function(message){
      // Let's get the domains from the the users the messages were from 
      // example: list:@example.com -> Result: example.com
      emailDomain = message.getFrom().split('<').pop().split('>')[0].split('@')[1];
      // if emailDomain is equal to example.com, then do something
      if(emailDomain === 'example.com'){
        Logger.log(message.getFrom());
      }
    });
  } 
}

使用Class GmailApp I got a certain label with the .getUserLabels() method and iterate through the threads thanks to the .getInboxThreads method. With a second loop and the .getMessages() you can get all the messages in a thread and for knowing the one who sent them, just use the .getFrom()方法。

文档

更多信息请查看:

所以我能够避免回复来自邮件列表地址的电子邮件,方法是使用 getRawContent() 方法然后在该字符串中搜索 "Mailing-list:"。到目前为止,该脚本运行良好。

function autoReply() {
      var interval = 5;    // if the script runs every 5 minutes; change otherwise
      var date = new Date();
      var day = date.getDay();
      var hour = date.getHours();
      var noReply = ["email1@example.com",
                     "email2@example.com"];

      var replyMessage = "Hello!\n\nYou have reached me during non-business hours. I will respond by 9 AM next business day.\n\nIf you have any Compass.com related questions, check out Compass Academy! Learn about Compass' tools and get your questions answered at academy.compass.com.\n\nBest,\n\nShamir Wehbe";
      var noReplyId = [];

      if ([6,0].indexOf(day) > -1 || (hour < 9) || (hour >= 17)) {
        var timeFrom = Math.floor(date.valueOf()/1000) - 60 * interval;
        var threads = GmailApp.search('from:@example.com is:inbox after:' + timeFrom);
        var label = GmailApp.getUserLabelByName("autoReplied");
        var repliedThreads = GmailApp.search('label:autoReplied newer_than:4d');

        // loop through emails from the last 4 days that have already been replied to
        for (var i = 0; i < repliedThreads.length; i++) {  
          var repliedThreadsId = repliedThreads[i].getMessages()[0].getId();
          noReplyId.push(repliedThreadsId);
        }

        for (var i = 0; i < threads.length; i++) {     
          var message = threads[i].getMessages()[0];
          var messagesFrom = message.getFrom();
          var email = messagesFrom.substring(messagesFrom.lastIndexOf("<") + 1, messagesFrom.lastIndexOf(">"));
          var threadsId = message.getId();
          var rawMessage = message.getRawContent();
          var searchForList = rawMessage.search("Mailing-list:");
          var searchForUnsubscribe = rawMessage.search("Unsubscribe now");

             // if the message is unread, not on the no reply list, hasn't already been replied to, doesn't come from a mailing list, and not a marketing email then auto reply
             if (message.isUnread() && noReply.indexOf(email) == -1 && noReplyId.indexOf(threadsId) == -1 && searchForList === -1 && searchForUnsubscribe === -1){
                message.reply(replyMessage);
                threads[i].addLabel(label);
          }     
        }
      }
    }