r/GoogleAppsScript • • May 14 '22

Question Trigger a Script on Email Receipt

Is it possible to trigger a script anytime an email is received to a specific address? My plan was to have certain emails that will need to be regularly processed forwarded to "[email protected]." In gmail, I made a filter to apply a label and star those emails (using the star to tell if it's been processed yet).

I was hoping to find a way to base a trigger on a gmail label or just any email receipt if needed. This is to avoid running a script and scanning the label every minute.

This page mentions...

An installable calendar event trigger runs when a user's calendar events are updated—created, edited, or deleted.

If there's no way to trigger based on emails directly, could calendar events be a workaround. That would require figuring out how to reliably create calendar events from emails, which is an additional question.

EDIT: In case anyone finds this, I was able to activate a Google Apps Script based on a specific received email trigger by publishing the script as a web app, using a webhook call triggered by certain emails with a free Zohomail account, have emails labeled as noted above in Gmail and forwarded to the Zohomail account.

If you just need the email as a trigger and don't actually need the email contents for any processing, then you could just skip the forwarding part and send the email to Zoho directly.

6 Upvotes

15 comments sorted by

View all comments

1

u/RemcoE33 May 14 '22

Why is checking every minute not desirable?

1

u/wioneo May 14 '22

It seems wasteful, because the emails will be sent at most a few times per week, but the times will be unpredictable and prompt processing is needed.

I assume there is some limit to how much processing time Google will allow each account and I'd rather avoid wasting it.

2

u/RemcoE33 May 14 '22

You can build this (outside the scope of apps script) via the watch method. Or you can setup an IFFF integration that has done the coding already. Then you can send a request to an apps script endpoint to activate the script.

1

u/wioneo May 14 '22

From reading into IFTTT (I think that's what you were talking about) a bit more, I learned that by deploying a Google script as a web app, it can be triggered via a URL request. That wasn't the exact thing that I was looking for, but I can think of several ways to automate a URL request.

1

u/RemcoE33 May 14 '22
  1. The mail will tigger IFTTT

  2. IFTTT will send a request to Apps script

  3. Apps script is invoked and will do the thing you want it to do.

This way you will have what you need right?

1

u/wioneo May 14 '22 edited May 14 '22

That should work, yes. I've never used IFTTT before, so now I'm trying to figure out how applets work exactly. Thanks for the suggestion.

Reading more into it, I might not need to use Google scripts at all, because it seems like IFTTT has cloud service integration.