1

I'm currently trying to automate an auto-reply with Google Apps Script for whenever I am out of office based on my Google calendar events named 'OOF'. Even if everytime I'm getting closer, there's always something that doesn't work. Now when I execute it, there's a new error message. See below:

ReferenceError: myEmail is not defined autoReply @ Code.gs:44

Please know that I'm no coder and I have a really basic knowledge of coding haha

Also, another thing to keep in mind is I prefer sending the auto-reply reply in a new thread of email instead of replying directly to an email. This is one of the reasons why I'm using Google Calendar to automate an auto-reply.

And I don't want to push my luck too much but is there any way to make the code below work if the Google Calendar is not an all day event (i.e.: 12pm to 6pm instead of all day)

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;
  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) {

        Logger.log("Replied now");

        // reply
        threads[i].reply("", {
          htmlBody: htmlbody
        });

        // label
        label.addToThread(threads[i]);

      }

    }

  }

1 Answer 1

0

Variable Not Defined

Per the message, line 44 of your code contains an error.

44   if (message.getFrom().indexOf(myEmail) < 0 && message.getFrom().indexOf("no-repl") < 0 && message.getFrom().indexOf("bounce") < 0 && message.getFrom().indexOf("spam") < 0) {

Specifically myEmail in message.getFrom().indexOf(myEmail) is not defined.

Generally speaking, in order to use any variable, it must first be defined.

Here an example showing a variable that is defined vs. one that is not.

Defined Variable

1  function myFunction() {
2    var myEmail=["[email protected]", 
3                 "[email protected]",
4                 "[email protected]"];
5    console.log(myEmail); 
6  }

Output

   12:23:12 PM  Notice  Execution started
   12:23:12 PM  Info    [ '[email protected]',
                          '[email protected]',
                          '[email protected]' ]
   12:23:12 PM  Notice  Execution completed

Undefined Variable

1  function myFunction() {
2
3
4
5    console.log(myEmail); 
6  }

Output

   12:23:12 PM  Notice  Execution started
   12:23:12 PM  Error   ReferenceError: myEmail is not defined
                        myFunction  @ Code.gs:6

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.