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/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();

}