I am using the following script from Any way to send Gmail auto-response at certain times every week?. It does not send the expected email.
function autoReply() {
var interval = 5; // if the script runs every 5 minutes; change otherwise
var date = new Date();
var day = date.getDay();
var hour = date.getHours();
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()){
threads[i].reply("I am out of office. We will get back to you shortly.");
threads[i].markRead();
threads[i].markImportant();
}
}
}
To test the script I sent emails from another address. I've tested many times with clicking run both before and after the emails were received. I don't believe that should make a difference because the script should run again after the specified interval.
The Execution log shows that the script completes immediately after it starts.
Edit: Removed if statement as per @doubleunary answer,
threads[i].reply()
. That indicates that thefor
loop is never iterated, which indicates thatthreads.length
is zero, which indicates thatGmailApp.search()
returns an empty array, which indicates that there are no messages in your inbox that match the criteria. Useconsole.log()
to write debug messages that you can use to inspectthreads.length
, and verify whether thefor
loop gets iterated.console.log
statements as was suggested by doubleunary in the previous comment, consider to use the debugger. If you need help to learn to use it, please post a new question.