Gmail – Any way to send Gmail auto-response at certain times every week

automationgmail

I have a Gmail account that it is only monitored Monday through Thursday. I would like to set something up to automatically send a canned reply for emails received between Thursday afternoon and Monday morning informing the sender that the email will not be seen until Monday morning, and giving emergency contact information.

I don't think this can be done directly in Gmail right now. (Vacation replies need to be manually turned on and off, and filters for canned email do not include any date/time options.) Can I set Gmail "out of office" replies for every week recurring? also suggests that this is was possible in Gmail, at least at the time of that question.

Is there any work-around or way to do some simple coding to accomplish this? I have a software development background, but I'm not aware of what (if any) options are available to the general public to extend Gmail.

Best Answer

Here is an Apps Script that does this. You can click this start scripting link to open script editor in Google Drive. Then:

  1. Replace the placeholder in the editor with the script given below.
  2. Go to "Resources > This project's triggers", and set a trigger to run autoreply every 5 minutes.

The logic of the script is explained below.

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();
  if ([5,6,0].indexOf(day) > -1 || (day == 1 && hour < 8) || (day == 4 && hour >= 17)) {
    var timeFrom = Math.floor(date.valueOf()/1000) - 60 * interval;
    var threads = GmailApp.search('is:inbox after:' + timeFrom);
    for (var i = 0; i < threads.length; i++) {
      threads[i].reply("I am out of office. Your email will not seen until Monday morning.");
    }
  }
}

The script only does something if the current local day/time is

  • Friday, Saturday, Sunday (days 5,6,0 in the part [5,6,0].indexOf(day)), or
  • Monday before 8 am (day 1, hour < 8)
  • Thursday after 5 pm (day 4, hour >= 17)

What it does is check all messages received in the last 5 minutes (interval in the script) and replies to each with a canned text.

It is important that the two time intervals match: the interval at which the trigger is set, and the one set in the script. Otherwise the script will either fail to reply to some messages, or will reply several times.

Remarks

You can try to run this every minute instead of every 5 minutes; but I wasn't sure if this would exceed the maximal running time allowed to scripts for a day (1 hour total running time).

I considered more sophisticated forms of this: with adding a label "auto-reply", or with storing the time of last run in ScriptProperties. But the above gets the job done, and wins in simplicity.