9

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.

8

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.

  • I was going to suggest something like ifttt.com but this is way better! – barrycarter Feb 19 '16 at 18:19
  • Thanks for this answer. I haven't had a chance to play with it, but assuming it works when I try it, I'll accept the answer then. – PurpleVermont Feb 23 '16 at 2:22
  • note that the Triggers menu item has moved to the Edit menu – userSteve Jul 27 '18 at 14:17
4

The script in user79865's answer is really great! I did notice, however, that when I sent a test message outside of my office hours, I kept replying to myself every 5 minutes until it became my office hours again. In order to work around that I edited the script slightly, adding an if statement to the for loop sending the responses:

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++) {
      if (threads[i].isUnread()){
      threads[i].reply("I am out of office. Your email will not seen until Monday morning.");
      threads[i].markRead();
      threads[i].markImportant();
      }
    }
  }
}

The responder will now mark a thread as read once it sends the reply, and should ignore all unread threads. In order to keep track of which emails were sent while I am out of the office, the responder also marks the thread as important, so I still know to send a real reply!

protected by Community Aug 17 '17 at 1:46

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.