Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google gmail script that triggers on incoming email

I've been reading through gmail addons. They have contextual triggers that trigger when you open an email.

Is it possible to trigger a service when an email is received by me? Best I can find is unconditional but that only triggers when the email is opened.

like image 914
Jeff Avatar asked Apr 26 '18 00:04

Jeff


People also ask

Does Gmail have email scripting?

If you know JavaScript, you can use Google Apps Script to add features to many Google products including Gmail. If you've used Office scripting, the idea is the same, although obviously the implementation is very different.

What is Gmail app Script?

Apps Script is a rapid application development platform that makes it fast and easy to create business applications that integrate with G Suite.


2 Answers

Yes, you can trigger a function for every new email!

Just use the search query newer_than:1h. Have your trigger run every 10 minutes for example. Then you will essentially be running logic for every new email (with up to 10 minutes delay, which is probably fine).

var TRIGGER_NAME = 'handleNewEmails'

// Maximum number of threads to process per run.
var PAGE_SIZE = 150

var INTERVAL = 10

function Install() {
    // First run 2 mins after install
    ScriptApp.newTrigger(TRIGGER_NAME)
        .timeBased()
        .at(new Date(new Date().getTime() + 1000 * 60 * 2))
        .create()

    // Run every 10 minutes there after
    ScriptApp.newTrigger(TRIGGER_NAME)
        .timeBased().everyMinutes(INTERVAL).create()
}

function Uninstall() {
    var triggers = ScriptApp.getProjectTriggers()
    for (var i = 0; i < triggers.length; i++) {
        if (triggers[i].getHandlerFunction() === TRIGGER_NAME) ScriptApp.deleteTrigger(triggers[i])
    }
}

function handleNewEmails() {
    var threads = GmailApp.search("newer_than:1h", 0, PAGE_SIZE)
    for (var i = 0; i < threads.length; i++) {
        var thread = threads[i]

        // Do something with the thread.
    }
}
like image 169
trusktr Avatar answered Sep 28 '22 12:09

trusktr


You can't create a trigger for every email, however you can do something similar as described in this answer.

For example you can:

  1. Set up a filter that puts a special label on incoming emails that you want to process.

  2. Set up a reoccurring script that runs every 10 minutes, or even every minute. In the script, you can pull all of the emails that have the given label, and process them accordingly, removing the label when you are done.

function processEmails() {
  var label = GmailApp.getUserLabelByName("Need To Process");
  var threads = label.getThreads();  
  for (var i = threads.length - 1; i >= 0; i--) {
    //Process them in the order received
    threads[i].removeLabel(label).refresh();
  }
}

You can then set this on a time based trigger to have it run as often as you would like.

If you want to keep track of the emails you have processed, you can create another "processed" label and add that to the message when you are done processing.

like image 35
ryan.d.williams Avatar answered Sep 28 '22 12:09

ryan.d.williams