Search code examples
google-apps-scriptgmail

Gmail service events?


I would like to run a processing function whenever a new email arrives in my inbox. I'm looking at https://developers.google.com/apps-script/guides/triggers/events . I don't see a way to trigger an event when an email arrives. Am I missing something?


Solution

  • There are no email-based triggers in Apps Script, as Serge insas said. As an imperfect workaround, you can run a script every 5 minutes and process the messages that arrived within the latest interval. Here is an example, based on this post:

    function checkEmail() {
      var interval = 5;    //  if the script runs every 5 minutes; change otherwise
      var date = new Date();
      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("This is auto-reply for demonstration; you probably want to do something else here.");
      }
    }