Google gmail script that triggers on incoming emai

2019-04-16 17:48发布

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.

1条回答
Ridiculous、
2楼-- · 2019-04-16 17:55

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.

查看更多
登录 后发表回答