How to trigger a Google Apps Script once an email get in the inbox?

Viewed 35957

I have created a Google Apps Script that checks if an email has an attachment then send it to another email address.

It's working fine, but I would like to create a trigger that would launch the script as soon as a new email arrives in the inbox.

I have been able to create a trigger that launch the script every hour, but it's not what I want

3 Answers

This answer is adapted from the answer of @AziCode but includes code.

Components of the solution:

  • A code-free Gmail filter that catches emails with the desired condition; for actions choose “star it” and “apply the label” (enter your desired label)
  • A trigger configured to run often; this trigger loops through your emails, catching ones that are starred and have the label, performing your desired action on them, and then removing the star.

Here’s the code to have in your Google Apps Script project (you must give the script permission to access your GMail inbox):

// Configure this trigger to run often
// (*how* often depends on the desired response time *and* how willing you are to risk hitting Google Apps Script’s rate limits;
// 10 minutes is probably good)
function triggerScriptForEmail() {
  const threads = GmailApp.search('is:starred label:"<your desired label>"');

  for (const thread of threads) {
    const messages = thread.getMessages()

    for (const message of messages) {
      // we know the thread *has* starred messages, but not *all* messages are necessarily starred
      if (message.isStarred()) {
        // THE CODE SPECIFIC TO YOUR CASE GOES HERE
        // THEN...
        message.unstar()
      }
    }
  }
}

You're probably better off running something like the following, it's not perfect and may need tinkering but this is how i'd achieve it:

function YourAutoEmailReplier() {
  var ureadMsgsCount = GmailApp.getInboxUnreadCount();
  if (ureadMsgsCount > 0) {
    var threads = GmailApp.getInboxThreads(0, ureadMsgsCount);
    for (var i = 0; i < threads.length; i++) {
      var message = threads[i].getMessages()[0]; 
      //this 0 above means it only replies to the first email message in reply chain
      //You can adjust this get the latest if you like
      var email = message.getFrom();
      var body = message.getPlainBody();
      var subject = message.getSubject();
      var date = message.getDate();
      date = date.toString();
      var fulldate = date.split(' ');
      var day = fulldate[0];
      var month = fulldate[1];
      var numday = fulldate[2];
      if (numday.charAt(0) === '0') {
        numday = numday.slice(1);
      }
      var year = fulldate[3];
      var time = fulldate[4];
      var brokentime = time.split(':');
      var hour = brokentime[0].replace(/^0+/, '');
      var minute = brokentime[1];
      var second = brokentime[2];
        if (subject.indexOf('RE:') > -1) {
          var subject = subject;
        } else {
          var subject = "RE: " + subject;
        }
        var fulldetails = email.split(' ');
        var firstname = fulldetails[0];
        var surname = fulldetails[1];
        var replyBody = "Hello there " + firstname + "!,\nThank you ever so much for your email at " + hour + " on " + day + " the " + numday + " of " + month + ". I will be sure to get back to you as soon as possible.\nKind Regards\n\n";
        GmailApp.sendEmail(email, subject, replyBody);
        message.markRead();
      }
    }
  }

Then go to Triggers -> Add Trigger -> Set your event source to time-driven -> select minutes timer and choose the least amount of minutes available to your account

Related