0

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,

3
  • 1
    You are saying that the code completes without calling threads[i].reply(). That indicates that the for loop is never iterated, which indicates that threads.length is zero, which indicates that GmailApp.search() returns an empty array, which indicates that there are no messages in your inbox that match the criteria. Use console.log() to write debug messages that you can use to inspect threads.length, and verify whether the for loop gets iterated. Commented May 20, 2021 at 19:31
  • Besides adding 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. Commented May 20, 2021 at 19:32
  • Questions should not be edited because a suggestion given as an answer. Doing so will invalidate the answer, instead if you have a follow-up question, please post a new question. Commented May 26, 2021 at 15:52

1 Answer 1

1

if ([5,6,0].indexOf(day) > -1 || (day == 1 && hour < 8) || (day == 4 && hour >= 17))

The function seems designed to only do its thing when it is run on a Friday (5), Saturday (6) or Sunday (0), or on a Monday (1) provided that the current time is before (8), or on a Thursday (4) provided that the current time is after 5 p.m.

Modify the conditions in the if statement to suit your requirements, or remove the if altogether to make the function do its thing on any day at any time.

See Date.getDay().

To avoid issues with timezones, try this pattern:

const timezone = 'GMT'; // replace GMT with your real timezone
const dayOfWeek = Utilities.formatDate(date, timezone, 'ddd');
const hour = Utilities.formatDate(date, timezone, 'HH');
if (['Fri', 'Sat', 'Sun'].indexOf(dayOfWeek) !== -1
  || (day == 'Mon' && hour < '08')
  || (day == 'Thu' && hour >= '17')) {
  //...

You should replace the 'GMT' bit with your real timezone. See Joda-time for a list of valid timezone names, and Utilities.formatDate() for more info on how to format datetime values as text strings.

6
  • Have you considered the effects of the time zone? Under certain circumstances Date methods might return values from an unexpected time zone. Commented May 19, 2021 at 17:58
  • 1
    Edited the answer to include a timezone pattern. Commented May 19, 2021 at 18:58
  • Thanks, but I still can't make it work. You were right about my intended schedule. I want it to run at all times. So I removed the if statement as per the code change in the question.
    – steveorg
    Commented May 20, 2021 at 15:34
  • The execution log has always shown a successful completion. I added the time-driven trigger and it now works. I tried to simplify the code by removing the interval, but that led to "trigger not defined" or other issues. Not a big problem but I'd appreciate a suggestion on how to fix it. I do have a bigger issue. I tried to use HTML in the reply content, but it was sent as text. Is there a way to use HTML?
    – steveorg
    Commented May 20, 2021 at 16:27
  • Thanks again. My current code is in the question.
    – steveorg
    Commented May 21, 2021 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.