Skip to content

Instantly share code, notes, and snippets.

@empeje
Created August 1, 2022 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save empeje/143bb8dbaf204e1f637aab5735372a78 to your computer and use it in GitHub Desktop.
Save empeje/143bb8dbaf204e1f637aab5735372a78 to your computer and use it in GitHub Desktop.
Autoreply Gmail out of office hours
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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment