Jeff
Jeff

Reputation: 4413

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.

Upvotes: 23

Views: 30588

Answers (4)

trusktr
trusktr

Reputation: 45454

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.
    }
}

Upvotes: 3

alexkovelsky
alexkovelsky

Reputation: 4190

Actually, they have somewhat complex pub/sub service that requires setting up OAuth authentication. With this service you'll must be able to get push notifications. But I also think its easier to set up a trigger to run every 10 or even 1 minute.

Upvotes: 1

Starfield Screensaver
Starfield Screensaver

Reputation: 49

I had a little trouble with getting the labels right so I'm including code to log your labels. I modified user3312395's code also to add new label also. Thanks for the original answer too!

function emailTrigger() {

  var label = GmailApp.getUserLabelByName('Name of Label to Process');
  var newLabel = GmailApp.getUserLabelByName('New Label Name');

  if(label != null){
    var threads = label.getThreads();
    for (var i=0; i<threads.length; i++) {
      //Process them in the order received
      threads[i].removeLabel(label);
      threads[i].addLabel(newLabel);
      //run whatever else here
    }
  }

}

function getLabels(){
  var labels = GmailApp.getUserLabels();
  for(i=0; i<labels.length; i++){
    Logger.log(labels[i].getName());
  }
}

Upvotes: 4

ryan.d.williams
ryan.d.williams

Reputation: 650

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.

Upvotes: 26

Related Questions