2

Because my clients bother me out of hours I would like to have my Out of Office respond out of hours without having to set it manually every day.

To comlicate matters further I want to send one message on between Friday 5pm and Monday 9am and a different message out of hours on all other weekdays.

How do I do that?

I am no coder, so do I just duplicate and adjust the IF statement (apologies if this is the wrong terminology) and adjust to suit?

Also, would I be correct in thinking that this sends a plain text response? I would like to send an HTML response so that it will display corporate logos etc. like a standard email reply.

  • To get images in the email footer I have been looking at the documentation Google provide but I am thinking about changing the last line of the IF statement to something like this: <code>for (var i = 0; i < threads.length; i++) { threads[i].reply(wkdaypmMessage,{htmlBody: "some text<BR><BR><img src="cid:imageKey" />", inlineImages: <img src="cid:imageKey" />});</code> My issue is that I can't understand how to implement it. – CraigC Jun 8 '16 at 12:18
  • Self-answered questions are allowed, and encouraged, but the question post should contain just the question and you should add an actual answer post with your solution. See also: It's OK to Ask and Answer Your Own Questions – ale Jun 8 '16 at 12:59
  • Thanks for the feedback. I have seen other questions were the OP was criticised for not updating the question when self-answered. Perhaps that person was wrong! – CraigC Jun 8 '16 at 14:13
2

Here is my answer to my own question.

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 on Monday morning.";
  var wkdayMessage = "Hi, Thank you for contacting us. The office is now closed. All emails will be attended to when the office re-opens in the morning.";
  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 (var i = 0; i < threads.length; i++) {
      threads[i].reply(wkendMessage);
    }
  }
  else if (hour < 9 || hour >= 17) {
    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);
    }
  }
}

I still need help to format the output so that there are images in the footer though :)

  • Should I post a new question about how to get inline images in the footer of the output using this script? Unsure of the etiquette here – CraigC Jun 8 '16 at 14:23
  • 1
    Yes, you should post a new question. This isn't a discussion forum. – ale Jun 8 '16 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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