Last active
August 9, 2021 08:25
-
-
Save emwdx/96bfb719e9937f4cd9bf4655b2dfe75c to your computer and use it in GitHub Desktop.
Out of Office Autoreply - Google Scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Out of Office Autoreply - Google Script code by Evan Weinberg (Twitter: @emwdx) | |
// This code was based on the code for the google script autoreply post here on stack overflow: https://stackoverflow.com/questions/38955161/google-gmail-script-time-auto-reply | |
function autoReply() { | |
var interval = 5; // Set to the number of minutes between runs of this script. You set this also in the trigger settings through the stop-watch tab. | |
var daysOff = [0,6] | |
var weekdayMessageDays = [1, 2, 3, 4, 5] // 1=Mo, 2=Tu, 3=We, 4=Th, 5=Fr, 6=Sa, 0=Su | |
var workingHours = [8,17]; // 0-24 | |
//Set your messages here. | |
var weekdayMessage = "***This was sent by a robot!*** \n\n Hello, \n\n Thank you for your message. To maintain work/life balance, I am responding to email between 8 AM and 5 PM during the week. I will get back to you after 8:00 AM tomorrow. \n\n Thanks for understanding. \n\n EMW "; | |
var weekendMessage = "***This was sent by a robot!*** \n\n Hello, \n\n Thank you for your message. To maintain work/life balance, I am responding to email between 8 AM and 5 PM during the week. I will get back to you after 8:00 AM on Monday. \n\n Thanks for understanding. \n\n EMW "; | |
var mondayMessage = "***This was sent by a robot!*** \n\n Hello, \n\n Thank you for your message. To maintain work/life balance, I am responding to email between 8 AM and 5 PM during the week. I will get back to you after 8:00 AM today. \n\n Thanks for understanding. \n\n EMW " | |
var date = new Date(); | |
var day = date.getDay(); | |
var hour = date.getHours(); | |
var isWorkday = (daysOff.indexOf(day) == -1) | |
var isOutsideHours = (hour < workingHours[0]||hour >= workingHours[1]) | |
var sendAutoreply = !isWorkday || isOutsideHours | |
var sentMessage = "" | |
//You can send two different messages for weekdays and weekends. | |
if(weekdayMessageDays.indexOf(day)!=-1){ | |
//Send the weekend message if it is Friday after the end of working hours. | |
if(day == 5 && hour >= workingHours[1]){ | |
sentMessage = weekendMessage | |
} | |
if(day == 1 && hour < workingHours[0]){ | |
sentMessage = mondayMessage | |
} | |
//Otherwise send the weekday message. | |
else{ | |
sentMessage = weekdayMessage | |
} | |
} | |
else{ | |
sentMessage = weekendMessage | |
} | |
if (sendAutoreply) { | |
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++) { | |
if (threads[i].isUnread()){ | |
var senderEmail = threads[i].getMessages()[0].getFrom().split("@")[1] | |
//Set the filter domain you want to use, if any, in the next line, but keep the asterisk. This is because of the way I get the domain from the string in line 36. | |
var isFromDomain = /my-work-email.com*/.test(senderEmail) // You can also just set isFromDomain to 1 to reply to emails from all domains. | |
if(isFromDomain){ | |
threads[i].reply(sentMessage) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment