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);
}
}
}
You just need to
- Create account here https://script.google.com/
- Create a new project
- Copy and paste the code
- Deploy the automation
- Setup time based trigger
- Voila, you get automation when someone reaches you during off hours.
Thank you!
Thanks for sharing, I will try it.
Thanks, let me know if you have any feedback!
Thank you so much for sharing this with us.
Glad that it useful for you.
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?
Hey thank you for reading my post, you can deploy it directly in the google script site. https://script.google.com/
How does one “deploy” though? Do you mind writing the steps for someone who has never used script.google before?
Hi Ari, I will consider it. But “deploy” is a button somewhere in Google Script, it might also named “save” or “publish” now.