r/GoogleAppsScript • u/wioneo • 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.
1
u/daweener23 May 14 '22
Calendar based event is going to be the best solve. I tried doing something along the same lines and had to end up doing a calendar based event to have the script check the email every 30 minutes
1
u/wioneo May 14 '22
How did you reliably create the calendar event?
1
u/daweener23 May 16 '22 edited May 16 '22
You can do it with or without code.
Without Code:
In the script editor, click the icon on the left that looks like a clock, this will bring you to the script triggers page. In the bottom right of the screen there will be a blue button that reads "+ Add Trigger". When you click that it will bring up an interface that you can use to set parameters for your trigger. You will want to change "Select Event Source" from "From Spreadsheet" to "Time-driven". This will change some of the options below. You will be able to change the type and interval parameters which is how you can make it weekly, daily, hourly, every 30 mins, etc.
With Code:
function setDropdownTrigger()
{
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger('myFunction')
.timeBased()
.everyMinutes(30).nearMinute(0)
.create();
}
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
The mail will tigger IFTTT
IFTTT will send a request to Apps script
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.
1
u/elcriticalTaco May 14 '22
There was a thing on github called "Gmail Push Notifications" or something like that. I'm not at work so I dont have the link sorry.
I was never able to get it to work because it required the whole web app authentication thing. So its doable, but there's a reason most of us end up scanning every minute lol
1
u/dimudesigns May 15 '22 edited May 15 '22
Gmail API Push Notifications. See documentation.
But its only really viable if you have a Google Workspace account (https://workspace.google.com/pricing.html). Gmail Push Notifications are not as restricted on apps tied to a private account Google Workspace account.
If you try to deploy a script that uses Push Notifications from a standard consumer account (ie. anything ending with @gmail.com) you'll run into issues with authorization.
Push Notifications require Pub/Sub integration, and for that you'll need to create a GCP cloud project and bind your script to it. Moreover, they also require restricted Gmail API scopes, which means if you want to use the script outside of Test Mode (oath tokens are only good for one hour in Test Mode), it will have to undergo a security assessment (and those cost a lot of money).
1
u/mdpattersonusmc Nov 01 '23
would there be a way to add a google task to the calendar based on a gmail filter?
4
u/rupjyotidev May 14 '22
I don't think there exists a trigger for new emails received.
I would still go with a time based trigger to run the script every 15 minutes (or less).
What the script would do?
Check the recent messages of this email sender using search call (returns threads). Need to save the previous checked time or last processed message id (better) in a sheet or properties service to avoid multiple processing.
Process the message accordingly.