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 comments10Replies
FletaSenger
5 months ago

I appreciate you for sharing it with us :)

Abdurrachman M
5 months ago

Glad that it useful for you.

ElijahDaniels
7 months ago

Thanks for sharing, I will try it.

Abdurrachman M
7 months ago

Thanks, let me know if you have any feedback!

SolomonHuerta
9 months ago

Thank you so much for sharing this with us.

Abdurrachman M
9 months ago

Glad that it useful for you.

Show more replies