【问题标题】:How to filter out all emails that came from a mailing list in Gmail如何过滤掉来自 Gmail 邮件列表的所有电子邮件
【发布时间】:2020-01-10 02:55:00
【问题描述】:

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

【问题讨论】:

  • 你试过from:*@example.com吗?
  • 我只想要从我的公司域中从邮件列表发送的电子邮件:地址不仅仅是与公司域匹配的任何电子邮件。我希望这是有道理的。谢谢!

标签: google-apps-script gmail


【解决方案1】:

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

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()' 以进行测试

【讨论】:

  • 我很欣赏这个答案,但我只是希望能够过滤掉来自谷歌邮件列表的电子邮件。本质上,我已经有一个自动回复脚本,我想添加一个逻辑,让它不回复来自邮件列表地址的任何电子邮件。
  • 哦,我以为你只是想从列表中接收电子邮件。
【解决方案2】:

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

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,我通过.getUserLabels() 方法获得了某个标签,并通过.getInboxThreads 方法遍历线程。通过第二个循环和.getMessages(),您可以在一个线程中获取所有消息,并且要知道发送它们的人,只需使用.getFrom() 方法。

文档

更多信息请查看:

【讨论】:

    【解决方案3】:

    所以我能够避免回复来自邮件列表地址的电子邮件,方法是使用getRawContent() 方法,然后在该字符串中搜索“邮件列表:”。到目前为止,脚本就像一个魅力。

    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);
              }     
            }
          }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-16
      • 2022-06-18
      • 2021-11-21
      • 2015-09-27
      • 2022-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多