Table of Contents
ToggleIntroduction
Overview
If you plan to be away from your Gmail account—say, a vacation or a place without access to the internet—you can set up a vacation responder in Gmail to automatically notify people that you won't be able to get back to them right away.
When the autoresponder is active, whenever somebody sends you an email, they'll receive an email response containing what you've written in your vacation auto reply.
That's all Gmail 101…
But what if you want to reply to your customer even while you are not on vacation?
(Like, for example, when your office is closed).
Well, there is a script that helps you to do that!
Video Tutorial
Setting up the Gmail AUTORESPONDER script
The instructions on this article work for Gmail or G Suite email accounts.
1 – Go to https://www.google.com/script/start/ and click on the ‘Start Scripting‘ button.
2 – Click New Script at the top left.
3 – Rename the project to whatever you want (where you see “Untitled project”) – this will be the app name.
4 – Replace the code in Code.gs with the code below:
function autoReply() {
var interval = 5; // if the script runs every 5 minutes; change otherwise
var wkend = [6,0]; // 1=Mo, 2=Tu, 3=We, 4=Th, 5=Fr, 6=Sa, 0=Su
var wkendMessage = "Hi, Thank you for contacting us. The office is now closed. All emails will be attended to when the office re-opens at 9:00am PST on Monday morning.";
var wkdayMessage = "Hi! Thank you for your email. The office hours are Mondays – Fridays 9-6pm PST. Thank you for your patience!";
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); } } }
function doGet() {
return ContentService.createTextOutput(”");
}
5 – Customize the message based on your needs.
The current message is a generic one that is set for offices that work from 09:00AM to 18:00PM.
6 – Customize the days based on your needs.
The current week day message will be sent from Monday to Friday.
7 – When the customization is completed, click Save and Run.
8 – Then click Publish > Deploy as Web App
9 – Authorize the script with your Gmail Account.
10 – Go back into “My Projects” on the dashboard
11 – Click the 3 dots icon next to the project name
12 – Select Triggers
13 – Click on Add Triggers button will appear at the bottom right
14 – Choose next options:
Choose which function to run – Autoreply
Which runs at deployment – Head
Select Event Source – Time-Driven
Select type of time based trigger – Minutes Times
Select Minute Interval – Every 5 Minutes
Failure Notification Settings – Notify me dail
15 – Click on save
16 – Test the script and verify it works.
Results
Voila! Your after working hours emails will be auto-replied for you!
Using this script will allow you to maintain high levels of customer service!
What do you think of this tutorial?
Article Title: How to set up a Gmail Auto Reply for Office Hours
Short Description: Learn how to set up a Gmail autoresponder that applies to certain parts of the day only.
Author: Ardian Lumi
Publisher - Orgnization: MemberFix
Publisher Logo:
User Review
0 (0 votes)
Now let's hear from you!
Are you using this script for your Gmail account?
Is this article helpful for you?
Tell us in the comments section below.
Well after spending HOURS and HOURS on trying to get my gmail auto reply working, there were some crucial steps that were missing in your guide, which would have saved me from so much stress! I want to help others who are also finding themselves in my situation by sharing what I did. I also found a way to add line spacing to the plain text which will at least provide a more semi-professional reply, instead of an extremely long line of unformatted text. Here is a copy of my working script: function autoReply() { var interval = 1; //… Read more »
Thanks for doing this! I’m not able to save due to a syntax error on line 6. What did I do wrong?
Hello Rebeca,
Please add the ” ; ” to the end of line 20
Let me know if it won’t help.
Thank so much for the information. I am unable to due to the following error. Can you tell how to resolve?
Thanks Melissa! Just curious – what time zone are you in? Is the example code based on UTC time (so PST would need to be offset by -480 minutes?)
I only ask because I pasted this code but would get automated replies BEFORE the end time I set.
Hey Try updating the google script timezone for the project check this out https://www.youtube.com/watch?v=nKIJchyAOus
Hey Try updating the google script timezone for the project check this out https://www.youtube.com/watch?v=nKIJchyAOus
My experience with this script as well as the original was that the unexpected or invalid token was the quotation marks. It’s not that they don’t belong, but for some reason, the marks in the examples on this web page don’t always get interpreted properly by the script editor. By replacing the marks with ones from my keyboard, the errors were resolved for me. This also applies to other punctuation like dashes (-) and semi-colons (;)
Thank you Melissa — Your script worked great after a few punctuation tweaks that did not translate well to the script editor from this web page. Both the original script and your tweaks copied quotation marks, dashes, and semi-colons to my editor that my editor didn’t want to recognize. After retyping those marks, the script worked well.
I did have to change the var Interval to 5 instead of 1 since I run every 5 minutes. Your carriage returns were just what I was looking for!!
I cannot get the last line to work. I’ve retyped all the semicolons and quotation marks. What am I doing wrong?
Hi — After setting Triggers, I got multiple replies to the same test email. How can I fix this?
When I tried to save the code, I got this: Syntax error: SyntaxError: Invalid or unexpected token line: 5 file: Code.gs
This is what line 5 shows: (copied from your code above):
var wkendMessage = “Hello and thank you for taking the time to contact us, however our office is currently closed for the weekend. \n”
Also, we’re in Eastern time zone. I’m trying to double check where I change the hours of operation.
BTW -I’ve programmed in BASIC, ages ago, done HTML, but this type of script is new for me.
Thank you!
How can I add an email exclusion list?
Thank you!
How can I add an email exclusion list?
I just did it. I tested it but it kept coming back to my email every 5 minutes with the same prompt. I changed it to 12 hours?
When I try to save I get the following error:
SyntaxError: Invalid or unexpected token (line 4, file “Code.gs”)
Hi Christie, Thank you for reaching out. It seems that the issue is related with the additional spaces that are added in the message. Can you please use the script below instead ( or you can copy the one in the article as I have updated and removed the spaces) : function autoReply() { var interval = 5; // if the script runs every 5 minutes; change otherwise var wkend = [6,0]; // 1=Mo, 2=Tu, 3=We, 4=Th, 5=Fr, 6=Sa, 0=Su var wkendMessage = “Hi, Thank you for contacting us. The office is now closed. All emails will be attended to… Read more »
used this exact code and am still getting a syntax error in line 5
I tried this script as i was getting the same error but now i get this error:
Syntax error: SyntaxError: Invalid or unexpected token line: 4 file: Code.gs
Hi Ardian, this is exactly what I have been searching for. Well done. Now, I have done everything and saved and Deployed as Web App. Would Gmail automatically pick this up? I sent a test message (it’s now Saturday afternoon). But I have not received an automated response. Please tell me what I have done wrong? Thank you.
(What do I do with this script?)
https://script.google.com/a/sixfoot5.co.za/macros/s/AKfycbwT7y50nPEGqaIx3Xp554uaItjoz6fmtnDsVKwXv0FwhUZm2P0/exec
Hi Ardian, I really need your help ASAP!
Hi Etienne,
I am glad to hear that you find this useful!
Unfortunately, I am unable to open the link you have sent.
Can you please check the timezone of your Google Account, perhaps that’s why you didn’t receive the email, furthermore can you try to send another email from a different account?
Feel free to reach out if you have any other questions.
Kind Regards,
Hello Adrian, thnx for the code.
But when I test the code, I get “Script function not found: doGet”
What do I need to add to the code?
Thnx
Hi Eric,
Apparently saving and republishing your google script web app is not good enough. You need to save a new version and publish the new version to make sure your app gets updated properly.
Please click Publish>Deploy as Web App> and select “New” in the project version dropbox.
Feel free to reach out if you have any other questions.
Kind Regards,
Hey, we get the same “Script function not found: doGet” error and don’t think it’s due to publishing it again. Looks like the script needs to have a doget function. We absolutely love your post Ardian, can you look at a solution? Thanks so much
http://googleappscripting.com/script-function-not-found-doget/
Hi Sheena,
Thank you for reaching out.
Unfortunately, I am unable to replicate the issue.
According to the article you have send you need to add the doGet() function.
I tried to add the doGet() function at the bottom and I didn’t see any errors. Please check the screenshot here: https://prnt.sc/siv84r
Try that method and I think you won’t see the error anymore.
Kind Regards,
Thanks for the quick reply Ardian. This definitely resolved the error. Unfortunately even after following your steps the script deployed, but didn’t seem to be running. I went in and setup a timed trigger for every minute and that seemed to do that trick. Thanks again for all of your help and for posting this. Really appreciate it
Hi Sheena,
I am glad to hear that is working fine now.
Kind Regards,
Hi there all, could you please test this script (email me) at etienne@sixfoot5.co.za and see if you get a response PLEASE? I would really appreciate this as there is no way of knowing!
Hi Etienne,
I have sent an email and received the auto response.
Have you received my email?
Kind Regards,
Can you post a video on how to do this–I am trying to set ours up. Thank you so much!
Hi Amy,
Thank you for reaching out.
We have created a video on how to do this, please check the video here: https://www.youtube.com/watch?v=OPZuGvOv7tI
Feel free to reach out if you have any other questions.
Kind Regards,
I am having the same issue, can you explain what you mean by a timed trigger?
Hi Kim,
I believe Sheena changed the interval from 5 to 1:
http://prntscr.com/sxgq9u
Please try to change that and check how it works for you.
Kind Regards,
sheena and Ardian–can you please post how to add the timed trigger to finish this tutorial.? I have got to the point where I have added lines
25. function doGet() {
26. return ContentService.createTextOutput(‘I just successfully handled your GET request.’);
27. }
What else is needed? It would be great if you can make a tutorial including the new steps needed to execute this task. Thanks so much!
Hi Amy,
If you see this error: “Script function not found: doGet”
By adding the doGet() function at the end as you have done the error will go away.
Please click Publish > Deploy as Web App > and select “New” in the project version dropbox.
It seems that the addition of doGet() function is necessary only for some users.
I was able to deploy the script without doGet() function.
Feel free to reach out if you have any other questions.
Kind Regards,
Hi!
Thanks for this. Really helpfull.
A quick question, is there a way to kinda temporarly stop the script without having to remove it?
Many thanks
Jose
Hi Jose,
Thank you for reaching out. I am glad that it works fine for you!
Unfortunately, there doesn’t seem to be a way to stop the script temporarily. The only way at the moment would be to remove the script and add it again when neccesary.
I hope this helps!
Kind Regards,
Hi Adrian! Thanks for the tutorial. BUt let me admit I have absolutely no clue about apps script. So I need your help to move further. Now that I have the script ready and error free, what are the next steps that i need to take to get my auto response going?
Hi Sanchita,
There are no additional steps. The script itself will run and auto-reply every email you get.
Feel free to reach out if you have any other questions.
Kind Regards,
This works like charm! Just curious if this involves any cost in terms of #times the script ran or something ?
This solution works like a charm ! However curious to know if there is any cost involved in terms of #times the script is run or anything like that.
Hi Abhay,
Thank you for reaching out. I am glad that it works fine for you!
This method is free of charge and you won’t be charged anything by using this script.
I hope this helps!
Kind Regards,
Hello- I get this message “The script completed but did not return anything.” any idea how I can resolve this?
Hi Maria,
I believe the doGet() function was empty that’s why you saw that error.
Can you please change the doGet() function slightly as shown in the screenshot here: https://prnt.sc/siv84r
So the script should be added as an addition to the other part of the script.
Kind Regards,
Thank you so much. Let me give it a try.
Hello- Thank you for troubleshooting with me. I no longer get that message.. now I get “I successfully handled your GET request.” However, I sent several test emails to see if I get the auto reply and nothing. I’m I doing something incorrectly? This is the doGet()fuction added-
Thank you for the help.
Hi Maria,
May I know if you tried to send the email after your working hours?
Also, it might take up to 5 minutes to receive the email, perhaps you got the email after a few minutes?
Also, is it possible to change the hours from 730 and 1530 to 7 and 15?
http://prntscr.com/su7la4
Can you please try again after making these changes.
Kind Regards,
function autoReply() { var interval = 5; // if the script runs every 5 minutes; change otherwise var wkend = [6,0]; // 0=Mo, 1=Tu, 2=We, 3=Th, 4=Fr, 5=Sa, 6=Su var wkendMessage = “Hi, Thank you for contacting me. My office is now closed. All emails will be attended to when I resume work at 9:00am AEST on Tuesday morning.” var wkdayMessage = “Hi! Thank you for your email. My office hours are Tuesday – Saturday 9-6pm. Thank you for your patience! I will contact you back during working hours”; var date = new Date(); var day = date.getDay(); var hour… Read more »
Hi Josh,
Can you please change the doGet() function slightly as shown in the screenshot here: https://prnt.sc/siv84r
So additionally this code should be added:
Please let me know if you have any other issues.
Kind Regards,
Hi Adrian!
The script is working perfectly, thank you for this amazing solution.
I’m ZERO code…and I would like to tweak a little the responder message. Maybe add my logo, change the paragraph style. Is there any way you can help me with this? Please feel free to contact me via email, but no during the weekend because I have an auto-responder! ha ha ha
Hi Angelo,
I am glad to hear that is working for you!
Unfortunately, the script doesn’t accept images at the moment and we can’t change the style of the paragraph, you can change the text though.
I know that this may not be quite the answer that you were hoping for, but please let me know if there is anything else that I can help with.
Kind Regards,
Hello Ardian and Sheena, I got same issue with Sheena. I tried the solutions that you give to sheena and it works. However, when I tried to sent email to the email address I created the script, the autoresponder is not sending the auto reply. May I know what is the timed trigger that I need to add in able for the autoresponder to work, please?
Hi Diana,
The auto-responder will work outside the working hours and during the weekend.
The script will trigger every 5 minutes.
Please let us know if you have any other questions.
Kind Regards,
Thank you for responding Ardian. I tried to test it during outside of working hours but it is not working. Did I missed some of the settings? Please help me. I posted this code: function autoReply() { var interval = 5; // if the script runs every 5 minutes; change otherwise var wkend = [6,0]; // 1=Mo, 2=Tu, 3=We, 4=Th, 5=Fr, 6=Sa, 0=Su var wkendMessage = “Hi, Thank you for contacting us. The office is now closed. All emails will be attended to when the office re-opens at 9:00am PST on Monday morning.” var wkdayMessage = “Hi! Thank you… Read more »
Hi Diana,
Can you please change the interval to 1.
http://prntscr.com/synump
It should be var interval = 1;
When you make the change please click Publish > Deploy as Web App > and select “New” in the project version dropbox.
Kind Regards,
I already changed it to 1. But nothing happens. I hope you can help me fix the problem. What should I choose in Who has access to the app:? Is it only Myself? or Anyone?
Hi Diana, What should I choose in Who has access to the app:? Is it only Myself? or Anyone? That is by default Only Myself, please leave it like that. I am sorry to hear that is not working for you. I would suggest you to delete the current Project and do the steps again from scratch. I think this is the only thing left because your script is working fine in my end. I know that this may not be quite the answer that you were hoping for, but please let me know… Read more »
Hi Diana, not sure if this is of any use but I came across ‘boomerang’ which lets you pause your inbox, sending out an auto-response, and can be scheduled to ‘un-pause’. I’m using that as an interim as I can’t get this to work. I pause it when i finish work and set it to come back on again the following morning. Works ok so far ????????
Thank you Debbie, I followed all the instruction of Ardian but it’s not working on our end. I’ll check this out and try it. Thanks!
Please help: I (thought) I’d followed your instructions but I’m also not getting an auto response when I send a test email from another account. Any ideas please?
Hi Debbie,
Can you please change the interval to 1.
http://prntscr.com/synump
It should be var interval = 1;
When you make the change please click Publish > Deploy as Web App > and select “New” in the project version dropbox.
Kind Regards,
Thanks – I did this but still nothing. Can you spot anything wrong? function autoReply() { var interval = 1; // if the script runs every 5 minutes; change otherwise var wkend = [6,0]; // 1=Mo, 2=Tu, 3=We, 4=Th, 5=Fr, 6=Sa, 0=Su var wkendMessage = “Hi, Thank you for contacting us. It is now outside working hours. All emails will be attended to on Monday morning. Enjoy your weekend!” var wkdayMessage = “Hi! Thank you for your email. Our working hours are Mondays – Fridays 9-4pm. Thank you for your patience!”; var date = new Date(); var day = date.getDay();… Read more »
Hi Debbie,
It seems that you have changed the line below as well:
https://prnt.sc/sz0b22
Can you please change it to:
if (wkend.indexOf(day) > -1 || (day == 5 && hour >= 16)) {
Thank you!
So I now have this and still can’t get it to work? Am desperate to get it to work today as I know a client is expecting to book an appointment today for tomorrow… function autoReply() { var interval = 1; // if the script runs every 5 minutes; change otherwise var wkend = [6,0]; // 1=Mo, 2=Tu, 3=We, 4=Th, 5=Fr, 6=Sa, 0=Su var wkendMessage = “Hi, Thank you for contacting us. It is now outside working hours. All emails will be attended to on Monday morning. Enjoy your weekend!” var wkdayMessage = “Hi! Thank you for your email.… Read more »
It’s overriding the automatic vacation reply that I just put on, but still not sending out an email. Does it make a difference that it tells me this at the top? This project is running on our new Apps Script runtime powered by Chrome V8
Hi Debbie,
I am sorry to hear that is not working for you.
I don’t see any issues on the script.
I would suggest you to delete the current Project and do the steps again from scratch.
I think this is the only thing left because your script is working fine in my end.
I know that this may not be quite the answer that you were hoping for, but please let me know if there is anything else that I can help with.
Kind Regards,
Thanks – I have tried removing it and starting from scratch but still no joy. Does the banner message at the top affect anything about runtime?
Hi Debbie,
Can you please use the exact same script that we have used and change only the working hours.
If there would be an issue with the message you would see an error that there is something wrong with the script.
If the script is published without any errors than the message is fine.
Thank you!
Yes – I have, over 20 times now 🙂 I then remove each one and start over. I do not get an error message at all. I was just asking about the fact that it says it is running on the new apps script and whether this would mean I had to update something somewhere. Clearly if the script is right, and others have managed to get it working, I must have a problem in the saving and running of it so am trying to figure out what as this would be SO helpful to me. Lockdown has blurred the… Read more »
How do you change it to have start and start and stop times that are not on the hour? I end my day at 3:30.
Hi Robert, In that case you need to change the code in if statements : https://prnt.sc/t1zm2k if (wkend.indexOf(day) > -1 || (day == 5 && hour >= 15)) { 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 >= 15) { 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); } } } So I… Read more »
Hello,
I’m currently off Sat- Mon. How do I edit this to include Mondays off?
How do I change the code to reflect my office hours? I know how to change the message, but how do I program it to only go out during my actual office hours?
Hi Like the idea a lot, and I wanna correct a bit, so I will make it read a xml file, that includes information whether I am at work or not. This xml I can write to from my phone, so in this way I can activate / deactivate auto-response from my phone. However, I don’t get the concept of the function and the interval. The main function is named autoReply() and the 2nd line is var interval = 5; But does Google just look for the interval variable and run the script every interval minutes? In my little developer… Read more »
.. and to reply to myself: I’ve found it, that there’s a (new?) feature called triggers, that you need to setup e.g. to make the function run e.g. every 5 minutes, and it should be available here https://script.google.com/home/triggers
Have fun! 🙂
I like this script and it works just fine, except now, it responds to ALL of my emails, even noreply emails. Is there a function I can add that will allow me to respond only to certain email domains, or something?
Hello Justin,
We will check it and if it’s possible we will update the article.
You can create a Google group with a list of all emails you want the response to send to and set the script to only send to users on that list through an if statement.
Hi Ardian,
Thank you for providing this tutorial, I have 2 questions.
Hello luis,
You also need to add minutes to your condition:
Where do you place this additional code in the grander code? I entered it where I thought it went and it stopped the automation from working altogether.
How do you change it to work for EST?
Hello Dani,
You need to add a time offset:
Oops meant to post this here: Where do you place this additional code in the grander code? I entered it where I thought it went and it stopped the automation from working altogether.
I’ve had different people emailing me to test it out. Some have had it work, one time it looked like I sent it to myself, and others just didn’t receive anything… How is that possible? Also, I’m on an all-staff list and it worked then. Is there a way to disable a particular list or spam email from getting the auto reply? function autoReply() { var interval = 5; // if the script runs every 5 minutes; change otherwise var wkend = [6,0]; // 1=Mo, 2=Tu, 3=We, 4=Th, 5=Fr, 6=Sa, 0=Su var wkendMessage = “Hi, Thank you for contacting me.… Read more »
Hi there, this is the script I wrote based on the information you provided. However, I keep getting an error: SyntaxError: Invalid or unexpected token (line 5, file “Code.gs”) Please let me know what I have done wrong. My business hours are from Monday to Friday from 10am to 5pm, I would need the below script / Auto Responder to run outside of those hours and including weekends. Please help me, I look forward to hearing back from you soon! Cheers, Amen info@resumeninjas.ca function myFunction() { function autoReply() { var interval = 1; // if the script runs every 5… Read more »
Ardian,
I am hoping you can help! When I test the web app for the latest code, I receive the error: Script function not found: doGet
Suggestions please?
Thank you so much,
Heather
Hello Heather,
Every webapp in Google Apps Script must have a main function called doGet() which is the entry point of the app, the function that your app will start with when you type the webapp URL.
This is true for every application deployed as a standalone app and called by its url – with a user interface or not.
So you need to just use a function called “doGet” from which you start your program execution.
I had published it already.
Why its not working. I still don’t have auto responsder?
I had to manually add a timed trigger to make it work (not in the code). Click Edit – current projects trigger – … and then add one from that page.
Worked a treat! Thanks Ardian
When i do this, it sends myself a copy of the email over an over again every minute. how do i prevent this from spamming my inbox with email?
Hello Seth,
Did you change anything on the script?
If yes can you send it?
same for me
same for me
here is my script
function autoReply() {
var interval = 5; // if the script runs every 5 minutes; change otherwise
var wkend = [6,0]; // 1=Mo, 2=Tu, 3=We, 4=Th, 5=Fr, 6=Sa, 0=Su
var wkendMessage = “Hello, Thank you for your email. The office is now closed. All emails will be attended to when the office re-opens at 8:30 am on Monday morning. Thank you for your patience!”
var wkdayMessage = “Hello, Thank you for your email. The working hours are Mondays – Fridays 8:30am -17:30pm. Thank you for your patience!”;
var date = new Date();
var day = date.getDay();
var hour = date.getHours();
if (wkend.indexOf(day) > –1 || (day == 5 && hour >= 17–30)) {
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 < 8–30 || hour >= 17–30) {
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);
}
}
}
function doGet() {
return ContentService.createTextOutput(“”);
}
Did anyone get a solution for this I’m getting that same issue?
The solution I found for this is once the auto responder is sent have your script mark the message as read. The script is set up to run for any new emails after a certain hour of the day, so if the email is not marked read the script will send the auto reply on a loop.
Hi, when I tried this I got this error ” This app isn’t verifiedThis app hasn’t been verified by Google yet. Only proceed if you know and trust the developer.” Please help
Hello Yandhira,
You need to click Advanced -> Go to… link.
You will find more information here: https://developers.google.com/apps-script/guides/client-verification
Hi Ardian,
I tried setting up the script and got this error: Authorization required
Out of Office Script needs your permission to access your data on Google.
I “reviewed my permissions”, was able to set it up, ran it as a web app, but I’m not getting any email responses when I send a test from another account.
Could you please help me figure out how to fix this? Thank you.
Additionally, when I test the app directly from the Script Dashboard I get this error: Script function not found: doGet.
Thank you.
Hello Belen,
Do you have the next function in the end of the script:
function doGet() {
return ContentService.createTextOutput(””);
}
If not, please add it and try again.
Unfortunately nothing works for me. can Someone please let us know how to set Office hours with autoreply google script?
I would greatly appreciate your support on this
Hello George,
What exactly doesn’t work? Do you have any errors?
Hello Victor
Thank you for coming back to this.
Following the updated post, it works!
Thank you very much in advance
George
HEllo Victor!
The email keep sending the autoreply message to himself everyminute.
Is this normal? haha
which trigger should i choose for not sending every minute?
Thank you so much
Hello George,
Please try to change this parameter:
Select Minute Interval – Every Minute
to
Every 5 Minutes
Also, it can be caused if you get an email from yourself on a day off or out of working time. It will launch the chain of emails that will be sent to your mail. Just eliminate it and this issue will be solved.
Thanks for this. how do i change to CST time?
Hello Justin
,
You need to add a time offset:
Hello Victor,
Thank you for coming back to this.
The main thing that confuses me, is that the autoreply operation does not responds to the mails. While testing the script that
Hello George,
So the script doesn’t work at all?
Yes after I also added this timezone offset code, the script won’t throw errors, but also doesn’t send any automated replies AT ALL, even after the set end time hours. I had the same issue
So after testing the getHours() function in W3schools (https://www.w3schools.com/jsref/jsref_gethours.asp) I found that the function pulled my local time. So is the offset for timezone necessary?
The offset code did not work for me (PST, -480), and the original code does work but works all the time; in other words, I receive auto replies before the actual end time I’ve set
Hi Victor, I am in CET +1 timezone, what do I need to add and where exactly in the code? thank you in advance hope to get an answer
script wortks but in working hours instead of out of office hours
I can’t get the script to work. It’s Saturday and I sent emails to all three of my email accounts with this script and I’m getting no auto response. I did everything stated in your instructions but it’s not working. Does this send a auto response on weekends??
Hello Chris,
Yes, it has to send an auto-response on Saturday and Sunday.
Did you change anything on the script?
Did you any errors while publishing the script?
Whoops…commented in the wrong spot. Sorry.
Hi there, I keep getting the same errors the others are getting.
My office hours are Monday to Thursday, 8:00am to 4:00pm EST and Fridays 8:00 to 12:00pm EST.
I keep getting an error that says “Syntax error: SyntaxError: Invalid or unexpected token line: 4 file: Code.gs”
Here’s my code:
function autoReply() {
var interval = 5; // if the script runs every 5 minutes; change otherwise
var wkend = [6,0]; // 1=Mo, 2=Tu, 3=We, 4=Th, 5=Fr, 6=Sa, 0=Su
var wkendMessage = “Hello, and thank you for contacting us. Our office is now closed. All emails will be attended to when the office re-opens at 8:00am EST on Monday morning.”
var wkdayMessage = “Hello and thank you for your email. Our office hours are Monday to Thursday, 8:00am to 4:00pm EST and Fridays 8:00am to 12:00pm EST. Thank you for your patience!”;
var date = new Date();
var day = date.getDay();
var hour = date.getHours();
if (wkend.indexOf(day) > –1 || (day == 8 && hour >= 16)) {
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 < 8 || hour >= 16) {
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);
}
}
}
function doGet() {
return ContentService.createTextOutput(”“);
}
Hello Mike,
You missed “;” in this line:
var wkendMessage = “Hello, and thank you for contacting us. Our office is now closed. All emails will be attended to when the office re-opens at 8:00am EST on Monday morning.”
Hi Everyone – can someone help me customize this to be sending one message from Monday to Sunday – outside the hours of 8am and 6pm? It works every weekday but I could not figure out how to eliminate the weekend conditions
Thank you!
Hello Mae,
You need to remove this condition:
Hi, thanks for this script. It was exactly what I was looking for.
But for some reason I received an auto reply myself every minute, woke up this morning to 1500 emails from myself.
And is it possible to exclude certain recipients from getting the auto reply message?
same for me. any suggestion?
me too!! how do we stop that!
I had the same problem… but I think I figured it out – When setting up your trigger, the settings above say “Select Minute Interval – Every Minute” but the code is written to run every five minutes… so it’s spamming our inboxes. Change your trigger setting to run every five minutes so it matches the script.
Hello Bea,
You are partly right. It’s because of the not synchronized script.
Also, you can start getting emails for each email you send to yourself. So you can eliminate this sequence and it will stop.
That doesn’t seem to work for me. Its lessened so I’m not getting an email duplicated to me every minute, but now it’s every 5 minutes.
Thanks for sharing this code!
I do have a question. I changed the closing hour to 16 (4pm) and sent myself an email from a different account BEFORE 4pm to test. It was 3:48pm and I received an auto reply.
Any suggestions on what happened and how to fix this?
This is what the code looks like:
if (wkend.indexOf(day) > -1 || (day == 5 && hour >= 16)) {
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 = 16) {
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);
}
}
*Note that I only changed 18 to 16 – everything else is exactly copied from above
if (wkend.indexOf(day) > -1 || (day == 5 && hour >= 16)) { 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 = 16) { 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); } } This is what I meant to paste – I'm honestly not sure how that code (else if(hour = 16)) was pasted from above. It doesn't… Read more »
I didn’t realize I needed to add the time offset, especially since the text in the example mentioned PST (which is where I am). I think the offset should be mentioned in the actual article, along with many examples. The comments were very helpful!
Hi Alexandra, would you be kind to share what I need to add to script to get the reply in my timezone? mine is CET or GMT+1 and my out of office reply started arriving in my working hours? I can not find where in the script and how to set the correct timezone? thank you in advance
hi, I have copy/pasted the code without an modification just to try it and I get this error:
Syntax error: SyntaxError: Invalid or unexpected token line: 25 file: Code.gs
image attached
after several tries, deleting code and getting it back it saved it….but now I ger replies in my working times instead of closing hours? where do I add the timezone?
thanks
It gives the same error for me in line 25. Could not solve it still, I need help!
I tried adding the timezone, thinking it had worked – but now I do not get any automated replies even AFTER the time. When I remove the code for the timezone offset, I get replies, but I also get replies BEFORE the end time I set. So I still don’t know what is going on. The offset is calculated in minutes difference from UTC (GMT), so since CET is one hour ahead it would be 60. It was recommended to paste this in the lines before the if/else statements, where you see var date = new Date(); (you will have… Read more »
I have made a script to automatically reply when I’m outside of working hours. The sender receive one message from me, as expected. However, I am getting an email with the same content every minute, until I actually read the customer email.
I do not want to mark the email as read until I actually read it.
Is there a solution of stopping sending these emails? Thank you!
Okay. So I have the script running, and it works. HOWEVER it keeps replying. To me.
So it replies to the initial email once , and then it keeps sending the reply to me (every minute), not the initial sender (luckily). I have added a code to label the emails that it replies to to be marked as read and important, which I think is why it at least isn’t replying to the sender of the original email.
It seems to me that there are many ‘doom loops’ in this code.
Example:
Adrian,
Thank you for posting this script. Just so your readers like me who do not know scripting can understand if they get errors at punctuation, if you copy and paste the script into your editor, the punctuation marks sometimes get misinterpreted by the editor. Just replace the red underlined marks with ones from your keyboard, and then they should work.
Thanks to Melissa who brought up the “\n” command to insert “carriage returns” (showing my age).
I wrote a script that switches your GMail Out of Office Autoresponder on and off depending whether there is an Out of the Office event in your calendar. (Note that OOTO Calendar events are not available for ‘free’ google accounts. This means that your script can use the GMail GUI for creating your OOTO message, you do not need to hard code it into your script. /** * Looks <interval> minutes into the past and the future for OOTO entries in the Calendar * <interval> currently set as 15 minutes in the code * This script should be triggered to run for double the interval setting (e.g. 30 minutes if interval is 15) * * Sets GMail Out-of-Office AutoReply ON if there is a current OOTO event in the primary calendar * or an OOTO is due to start within the next <interval> * Aligns the GMail Vacation start and end date with the Calendar OOTO date/timestamp * * Sets GMail Out-of-Office AutoReply OFF if there is no current OOTO event in the calendar * or an existing one will end during this <interval> * * The script DOES NOT CHANGE the GMail vacation responder options for domain emails and the user's Contacts * Behaviour of the above is not what you might expect * See https://support.google.com/a/thread/103802728?hl=en * The script DOES NOT CHANGE the GMail vacation responder subject title or message contents * These should be set manually by the user in the GMail settings * * NOTE: Gmail uses rounding to display vacation start and end times to the nearest DAY * in the GMail settings GUI. This script sets the start and end times to be identical to * the OOTO event start and end time. Gmail settings GUI do NOT display these as you might expect * but testing has shown that they are being used correctly * * PROVIDED WITHOUT WARRANTY OR SUPPORT * */ function syncCalendarOOTOWithGMailOOTOAutoReply () { var interval = 15 // minutes /** * Get the API response for events between * <interval> minutes in the past * and <interval> minutes in the future */ var now = new Date();… Read more »
How can I set this up to respond through the default email address for the account?
This was very helpful, thank you!
Now if only there were a way to link it to a series of calendar events?
This helps me lot. Thank you! What could I do if I don’t want to autoreply emails from specific email address?
Hello, the script has no errors but once someone has emailed me in the allotted time, it autoreply’s to me(from me) afterwards sending me up to 100 emails stating my after hours message, here’s my code. Not sure if i did something wrong. It seems if the email is left unread, then it Autoreply’s to myself until I’ve opened the email. function autoReply() { var interval = 5; // if the script runs every 5 minutes; change otherwise var wkend = [6,0]; // 1=Mo, 2=Tu, 3=We, 4=Th, 5=Fr, 6=Sa, 0=Su var wkendMessage = “Hello and thank you for taking the… Read more »
Thank you for this! It works great! However, the auto-reply message is sent out in boring Arial font. Is there a way to change the font of the auto-reply to the font we have set for our e-mail?
It seems like the fatal flaw of this script is the continuous looping of auto reply messages. For example I received an email that it auto responded ot, and then that email itself had an auto responder, then this script replied back to that… you can see where I’m going with this. I’m not sure if there is a way to make it only respond ONCE per sender like the actual vacation responder does, otherwise this is useless.
I made a much more betterer one after finding the same flaw. Plugged it together from multiple sources, and lets you customise the message with a HTML file in your Drive root. You need two triggers though: https://stackoverflow.com/a/68458557 I just can’t figure out why I can’t embed a base64 image into it though. function initializeLogs(){ //Get Script properties var scriptProperties = PropertiesService.getScriptProperties(); scriptProperties.setProperty('email',''); } function autoReply() { //Get Script properties var scriptProperties = PropertiesService.getScriptProperties(); var emailLogs = scriptProperties.getProperty('email'); Logger.log(emailLogs); var interval = 1; // if the script runs every 5 minutes; change otherwise var wkend = [6,0]; // 1=Mo, 2=Tu, 3=We, 4=Th, 5=Fr, 6=Sa, 0=Su // get html message var files = DriveApp.getRootFolder().getFilesByName('autoreply.html'); var htmlbody; while (files.hasNext()) { var file = files.next(); htmlbody = file.getBlob().getDataAsString('utf8'); } var date = new Date(); var day = date.getDay(); var hour = date.getHours(); if (wkend.indexOf(day) > -1 || (day == 5 && hour >= 17)) { var timeFrom = Math.floor(date.valueOf()/1000) - 60 * interval; var threads = GmailApp.search('is:inbox after:' + timeFrom); for (let mess of threads) { if (mess.getMessageCount() == 1) { //get email address of the message sender var sender = mess.getMessages()[0].getFrom(); if(!emailLogs.includes(sender)){ Logger.log(sender); //sender not yet in the logs, send reply mess.reply("", { htmlBody: output }); //add sender to the logs scriptProperties.setProperty('email', emailLogs+sender) } } } } else if (hour < 8 || hour >= 17) { var timeFrom = Math.floor(date.valueOf()/1000) - 60 * interval; var threads = GmailApp.search('is:inbox after:' + timeFrom); for (let mess of threads) { if (mess.getMessageCount() == 1) {… Read more »
Jeremy, thanks for your code. It saved with no error. So how do I create the HTML file, I have never worked with root drive and can barely code. I can copy and paste though! Steve
No worries Steve,
It’s just a web html file called “autoreply.html” saved in the top level of your Drive.
By that, I mean it’s not in any folders, and it’s just there under the Files section.
Hi, Jeremy. I tried using your code. It sent an autoreply but the email reply was empty. What could have been the problem?
Hi, Jeremy. I tried using your code. It sent an autoreply but the email reply was empty. What could have been the problem?
Could please you help me with the changes that would be needed to have this run only on Fridays between 8am and 5pm?
NEED HELP: My time zone is UTC +8 but I have to follow the PST time, what can I do to make the code follow the PST time?
Thank you for this.
Work like a charm. However, I have a question. I have a system that creates tickets from emails and I’m creating a loop when the script is working, how could I make this to work with all the incoming emails, except for this particular email address?
Thank you
Hi, I work Tues-Thurs, 8:15am to 5pm, so I’ve adapted the script. But then noticed that at my work email I got 17 messages in 17 minutes about the email I had sent to my work email from my personal email, Any ideas? function autoReply() { var interval = 5; // if the script runs every 5 minutes; change otherwise var wkend = [1,5,6,0]; // 1=Mo, 2=Tu, 3=We, 4=Th, 5=Fr, 6=Sa, 0=Su var wkendMessage = “Hi. Thank you for contacting me. As I work part-time, I will attend to emails during my working days (Tuesdays, Wednesdays & Thursdays).”; var wkdayMessage… Read more »
Solved. Set trigger to 5 mins in Google settings to match the script.
Can anyone help me alter the script for responding different times Saturdays and Sundays? My office is open 8am-12pm Saturday and closed Sunday.