0

I'm currently trying to automate an auto-reply email with Google Apps Script for whenever I receive an email and there's an event named 'OOF' in my Google calendar. I have very basic knowledge of coding and I've learnt a lot in the last two weeks about Google Apps Scripts however I would need an expert help. I'm getting closer to have the magic code but I can't make it work the way I want to.

The issue I'm facing now is that my auto-reply email is being send in the email thread instead of simply sending a new email to the person that emailed me to advice them I'm out of the office.

See below the code I've been using:

function autoReply() {

  Logger.log('Script start');
  var strCalendarOOFKey = 'OOF';
  var strUserEmailToSetVacationOn = '[email protected]';
  // find calendar event
  var today = new Date();
  var msTodayStart = today.getTime();
  var msTodayEnd = today.getTime()+60000;
  var unavailableToday = false;
  var myEmail="[email protected]";
  console.log(myEmail); 

  Logger.log('Looking for Calendar trigger "'+strCalendarOOFKey+'" for today '+today.toDateString());
  var events = CalendarApp.getDefaultCalendar().getEventsForDay(today, { search: 'OOF' });
  for (var i = 0; i < events.length; i++) {
    if(events[i].isAllDayEvent() && events[i].isOwnedByMe()) {
      Logger.log("Setting vacation response to true due to event titled '"+events[i].getTitle()+"'")
      unavailableToday = true;
      break; 
    }
  }
  
  Logger.log("Setting Vacation response to: "+unavailableToday.toString());
  
  var jsonVacationSettingsOn = {
    "enableAutoReply": true,
    "responseSubject": "I'm currently out of the office",
    "responseBodyPlainText": "If you need any help while I'm gone contact [email protected]",
    "responseBodyHtml": "If you need any help while I'm gone contact [email protected]",
    "restrictToContacts": true,
    "restrictToDomain": false,
    "startTime": msTodayStart,
    "endTime": msTodayEnd

  }

 var interval = 2;
    var date = new Date();
    var timeFrom = Math.floor(date.valueOf()/1000) - 60 * interval;
    var threads = GmailApp.search('is:inbox !label:autoresponded after:' + timeFrom);
    var label = GmailApp.getUserLabelByName("autoresponded");

    for (var i = 0; i < threads.length; i++) {
  
      var message = threads[i].getMessages()[0];
      if (message.getFrom().indexOf(myEmail) < 0 && message.getFrom().indexOf("no-repl") < 0 && message.getFrom().indexOf("bounce") < 0 && message.getFrom().indexOf("spam") < 0) {
    

    
      }
  
   }

 }

Anyone has any ideas on how to make my code work?

3
  • Is there some code missing?
    – Blindspots
    Oct 26, 2022 at 21:47
  • 1
    HI, welcome to WebApps. @BlindSpots is correct - you haven't included all of your code. For example, you define jsonVacationSettingsOn but never call it; there's no "send" command for your email; there are probably other things too. If we're to help, we need to see your working, though "unsuccessful", script. Would you please edit your question to include ALL of your code.
    – Tedinoz
    Oct 30, 2022 at 4:09
  • @Tedinoz when you take this question in combination with her question of 7 days prior, it is clear this is off-topic for Web-Apps. Maybe Stack Overflow if she cared to invest more time.
    – Blindspots
    Nov 1, 2022 at 19:23

1 Answer 1

0

You want to use the auto-reply feature to create a new message in a NEW thread. This is NOT way that auto-reply works - auto-reply is designed to add a new message to an existing thread.

  • You can't use auto-reply
  • You should use an installable time-driven trigger (paying close attention to the daily quota).
  • build the arguments for a "conventional" email,
  • add additional "topical" arguments from the received email and IF your Calendar testing proves correct,
  • send a new email (creating a new message thread).

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.