javascript - 如何在 Google Apps 脚本中忽略自动回复指定的电子邮件

标签 javascript google-apps-script gmail

我正在尝试添加逻辑,使我能够跳过保留在数组中的电子邮件列表,以免在 Google Apps 脚本中自动回复。我认为问题出在最后的 if 语句的条件语句上,但无法弄清楚。如果我向我的收件箱发送 3 封电子邮件,其中 2 封来自省(introspection)略列表上的地址,并运行脚本几次,它只会发送到它应该发送的一封并且工作正常。一旦另一封电子邮件出现在第四封邮件中,并且该邮件也不在省略列表中,它最终会回复该邮件以及省略列表中的另外两封邮件。非常感谢任何帮助,谢谢!

  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 = ["ls@compass.com", "feedback@compass.com", "jenkins@compass.com", "shamirwehbe@me.com", "shamirwehbe@yahoo.com"];
  var fromEmails = [];
  var yesReply

  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";

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

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

      var messagesFrom = threads[i].getMessages()[0].getFrom();
      var email = messagesFrom.substring(messagesFrom.lastIndexOf("<") + 1, messagesFrom.lastIndexOf(">"));
      fromEmails.push(email);

      yesReply = fromEmails.filter(function(e) {
        return noReply.indexOf(e) ==-1;});


        for (var y = 0; y < fromEmails.length; y++) {

         if (threads[i].isUnread() && yesReply.indexOf(fromEmails[y]) > -1){

            threads[i].reply(replyMessage);
            threads[i].markRead();
            threads[i].addLabel(label);

        }
      }               
    }    
  } 
}

最佳答案

认为您的处理方式不正确,但如果我错了,请纠正我。我看到的问题是您正在循环线程,并在同一循环内发送回复。这是一个奇怪的结构,我认为你可以简化它。

无需构建 yesReply 数组,只需检查电子邮件地址是否在 noReply 数组中并进行相应回复即可。

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 = ["ls@compass.com", "feedback@compass.com", "jenkins@compass.com", "shamirwehbe@me.com", "shamirwehbe@yahoo.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";

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

    for (var i = 0; i < threads.length; i++) {
      var thread = threads[i];
      var messagesFrom = thread.getMessages()[0].getFrom();
      var email = messagesFrom.substring(messagesFrom.lastIndexOf("<") + 1, messagesFrom.lastIndexOf(">"));

      if (thread.isUnread() && noReply.indexOf(email) == -1) {
        thread.reply(replyMessage);
        thread.markRead();
        thread.addLabel(label);
      }              
    }    
  } 
}

关于javascript - 如何在 Google Apps 脚本中忽略自动回复指定的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59340734/

相关文章:

javascript - 带有 JavaScript 的树在 Internet Explorer 7 上无法正常工作

javascript - 海妖网站 API : Generate the message signature in Google App Script

Python-Gmail邮件检索/下载

javascript - 动态 HTML 行 - 在 TR 上添加 id

javascript - Socket.io,广播发射,似乎仅使用最新连接的套接字

google-apps-script - 强制重新授权 Google 附加组件的范围

google-apps-script - 谷歌表单中的 e.values 会跳过空答案,有解决方法吗?

java - 如何使用 Gmail API 检索电子邮件正文内容?

python - 使用 gmail smtp 从我的 python 脚本发送报告

java - 使用 GWT ScriptInjector 加载脚本时如何设置 CacheHeaders?