Automate Gmail vacation / out of office setting

A colleague of mine asked if there was a way to automate turning on the auto-reply vacation feature of gmail. For example, he wanted it to send a standard reply to any incoming e-mails if they arrived when he wasn’t at work.

The auto-reply feature is also known as “vacation mode” or “out of office reply.” It looks like this, under gmail settings:

This is usually a manual setting, but he wanted it to turn on at a specific time of day, every day, without having to do anything.

I looked around, and didn’t find an easy setting or add-on to automate this setting in gmail.

So I tried my best to write one. I don’t really have much experience with javascript, but I was able to piece together enough to get it going, using Google’s App Script feature.

Here’s a quick video tutorial on how I set this up:

This should work on regular gmail, as well as enterprise or education versions. I’ve tried it on a couple of my enterprise clients, and it works!

Here’s how I solved this:

First, I looked at gmail API, which has this setting:

https://developers.google.com/gmail/api/reference/rest/v1/VacationSettings

And a corresponding method:

https://developers.google.com/gmail/api/reference/rest/v1/users.settings/updateVacation

With that in hand, I was able to create a script over at https://script.google.com

 
 

 
 

You can copy and paste my script below if you’d like to try it out:

 

function autoReplySwitcher() {
var date = new Date();
var day = date.getDay();
var hour = date.getHours();
if (hour < 8 || hour >= 16 || day == 0 || day == 6) {
    outOfOffice();
    } else {
    inOffice();
  }
}

function outOfOffice(){
    Gmail.Users.Settings.updateVacation(
      {
        "enableAutoReply": true,
        "responseSubject": "Out of Office",
        "responseBodyPlainText": "Hi there.  Your e-mail was received.  I will read and respond during working hours.  Thanks",
        "restrictToContacts": false,
        "restrictToDomain": false
      },
      'me'
    );
}

function inOffice(){
    Gmail.Users.Settings.updateVacation(
      {
        "enableAutoReply": false,
        "responseSubject": "in the Office",
        "responseBodyPlainText": "I'm back in the office",
        "restrictToContacts": false,
        "restrictToDomain": false
      },      
      'me'
    );
}

 

You’ll need to enable the gmail API, as well as give the script permission to change your gmail settings. Watch the video above for a walkthrough on how to do that.

The code checks to see if it’s before 8am or after 4pm, or if it’s Saturday or Sunday. If it is, it sets the vacation responder with a message saying that the e-mail was received, etc.

If the script is run during weekday, then it removes the autoresponder.

After publishing, I set the script to run automatically using triggers feature.

 

 

I set this script to run every 15 minutes. Just in case something goes wrong, it will try again in 15 minutes. I’m not sure if that’s good practice or not, but having it trigger at a certain time every day seemed to fail for me. So this is my low-tech way of correcting for errors. Just run it over and over again :slight_smile:

I hope this helps someone do something similar, or a starting point for customization.

1 Like

Mr. Bowie has been looking for this one since last year.