0

I have a script that I have adapted to suit my needs.

function autoReply() {
  var interval = 5;    //  if the script runs every 5 minutes; change otherwise
  var wkend = [6,0];   // 1=Mo, 2=Tu, 3=We, 4=Th, 5=Fr, 6=Sa, 0=Su
  var wkendMessage = "Hi,\n\nThank you for contacting us.\n\nThe office is now closed.\n\nAll emails will be attended to when the office re-opens at 9:00am on Monday morning.";
  var wkdayamMessage = "Hi,\n\nThank you for contacting us.\n\nThe office is now closed.\n\nAll emails will be attended to when the office re-opens at 9:00am.";
  var wkdaypmMessage = "Hi,\n\nThank you for contacting us.\n\nThe office is now closed.\n\nAll emails will be attended to when the office re-opens in the morning.";
  var date = new Date();
  var day = date.getDay();
  var hour = date.getHours();
  if (wkend.indexOf(day) > -1 || (day == 5 && hour >= 17)) {
    var timeFrom = Math.floor(date.valueOf()/1000) - 60 * interval;
    var threads = GmailApp.search('is:inbox -label:social-media after:' + timeFrom);
    for (var i = 0; i < threads.length; i++) {
      threads[i].reply(wkendMessage);
    }
  }
  else if (hour < 9) {
    var timeFrom = Math.floor(date.valueOf()/1000) - 60 * interval;
    var threads = GmailApp.search('is:inbox -label:social-media after:' + timeFrom);
    for (var i = 0; i < threads.length; i++) {
      threads[i].reply(wkdayamMessage);
    }
  }
  else if (hour >= 17) {
    var timeFrom = Math.floor(date.valueOf()/1000) - 60 * interval;
    var threads = GmailApp.search('is:inbox -label:social-media after:' + timeFrom);
    for (var i = 0; i < threads.length; i++) {
      threads[i].reply(wkdaypmMessage);
    }
  }
}

(Here is where I found the original script Any way to send Gmail auto-response at certain times every week?)

I added a Gmail label filter to the script because the script would get stuck in a loop with emails from certain social media companies that have a "noreply" return email address.

I thought that this would resolve the issue, but apparently not.

Can anybody help me work out what I have done wrong here?

  • 1
    What do you mean by "apparently not", what is happening with those messages? Does the script reply despite the messages having label social-media? Or do they not have that label? – user79865 Jun 15 '16 at 7:36
  • Exactly that. Despite the email arriving in the inbox and being labelled Social Media the script still sends an autoresponse. – CraigC Jun 16 '16 at 9:43
  • 1
    Very odd. Are those messages one in a thread, or more than one? Also, does searching is:inbox -label:social-media directly in Gmail UI show those messages? – user79865 Jun 16 '16 at 9:45
  • Are you still having this issue and able to provide more details? – Rubén Oct 12 '16 at 23:00
  • Yes, the issue still exists. The only workaround I could figure out to end the autoresponder ping-pong was to create a filter that looked for keywords e.g. "re: re:" associated with autoresponses and mark them as archived so that they skipped the inbox. Of course this creates the occasional false positive, and so I also label these emails to ensure that the autoresponses are true. If anybody has a more succinct way of achieving accurate detection or functionality then I would love to know more. – CraigC Oct 18 '16 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.