Search code examples
javascriptgoogle-apps-scriptgmail

How do I omit specified emails from being auto replied to in a Google Apps Script


I'm trying to add in logic that will allow me to skip a list of emails that I keep in an array from being auto replied to in my Google Apps Script. I believe the issue lies in the condition statement of the if statement at the end but can't figure it out. If I send 3 emails to my inbox, 2 of which are from addresses on the omit list and run the script a few times it only sends to the one it should and works perfectly. As soon as another email comes in a 4th that is also not on the omit list it ends up replying to that one and the other two from the omit list as well. Any help is greatly appreciated, thank you!

  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 = ["[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]"];
  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);

        }
      }               
    }    
  } 
}

Solution

  • I think you were approaching this incorrectly, but please correct me if I'm mistaken. The issue I see is that you're looping through the threads and, within the same loop, sending the replies. That's an odd structure and I think you can simplify it.

    Instead of building a yesReply array, just check if the email address is in the noReply array and reply accordingly.

    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 = ["[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]"];
      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);
          }              
        }    
      } 
    }