Codementor Events

Google Script to autoreply email during off-hours

Published Aug 01, 2022
Google Script to autoreply email during off-hours

Recently I was amazed by my colleague in europe where they basically reply me with autoreply during after hours. I want to replicate their automatin but there's no native way to do it in Gmail, so I leard that I can do Google AppScript here's the results

function autoReply() {
  var interval = 5;
  var wkend = [6,0];
  var wkendMessage = "Hi! Thank you for contacting me. I'm currently out of office. All emails we'll be replied approximately on next Monday.";
  var wkdayMessage = "Hi! Thank you for your email. I'm currently offline. I'll be back Mon-Fri 9-6PM UTC+2. You are getting this email because it is outside business hours.";
  var date = new Date();
  var day = date.getDay();
  var hour = date.getHours();

  if (wkend.indexOf(day) > -1 || (day == 5 && hour >= 18)) {
    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(wkendMessage);
    }
  }
  else if (hour < 9 || hour >= 18) {
    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(wkdayMessage);
    }
  }
}

See the complete link here.

You just need to

  1. Create account here https://script.google.com/
  2. Create a new project
  3. Copy and paste the code
  4. Deploy the automation
  5. Setup time based trigger
  6. Voila, you get automation when someone reaches you during off hours.

Thank you!

Discover and read more posts from Abdurrachman M
get started
post commentsBe the first to share your opinion
ElijahDaniels
21 days ago

Thanks for sharing, I will try it.

Abdurrachman M
20 days ago

Thanks, let me know if you have any feedback!

SolomonHuerta
3 months ago

Thank you so much for sharing this with us.

Abdurrachman M
3 months ago

Glad that it useful for you.

Ari Horowitz
a year ago

This is super helpful, thank you! Dumb question as I’ve never used google script before… how do you “deploy” this and get it to run?

Abdurrachman M
a year ago

Hey thank you for reading my post, you can deploy it directly in the google script site. https://script.google.com/

Ari Horowitz
a year ago

How does one “deploy” though? Do you mind writing the steps for someone who has never used script.google before?

Abdurrachman M
3 months ago

Hi Ari, I will consider it. But “deploy” is a button somewhere in Google Script, it might also named “save” or “publish” now.

Show more replies