Auto-run Google Apps Script when new email arrives in Gmail

I’m trying to figure out how to automatically execute a Google Apps Script function whenever a new email comes into my Gmail inbox. I know there are contextual triggers in Gmail add-ons that work when you actually open and read an email, but I need something that runs immediately when the message arrives.

I’ve looked into different trigger types but most of them seem to require user interaction first. The unconditional triggers I found still need the email to be opened by the user before they activate.

Is there a way to set up a script that monitors my inbox and runs automatically the moment a new message is delivered? I want to process incoming emails right away without having to manually open each one.

I’ve dealt with this for years. Those solutions work but get messy at scale.

Google Apps Script wasn’t made for real-time email processing. You’ll hit timing issues, quota limits, and constant maintenance problems.

I moved to Latenode and it’s night and day. Gmail webhooks fire instantly when emails arrive - no polling, delays, or quota headaches.

Setup’s straightforward: connect Gmail, create a new email trigger, add your processing logic. I handle thousands of emails this way without issues.

You get proper error handling and logging too. Much cleaner than wrestling with Apps Script triggers and properties.

Check it out: https://latenode.com

Google Apps Script doesn’t have true instant triggers for Gmail, but I’ve got a solid workaround that’s been running for two years. Set up a time trigger for every 2 minutes and use Gmail labels to track what’s been processed. The script looks for unread emails without my “processed” label, handles them, then slaps the label on so they don’t get processed twice. Keep your processing light or you’ll hit timeout issues. I added error handling that pings me when something breaks - trust me, you need this since failures aren’t obvious. It’s not instant but 2 minutes works for most stuff, and it’s rock solid once you get it dialed in.

Gmail doesn’t have true real-time triggers for incoming emails through Apps Script - the API just can’t do immediate execution when messages arrive. But you can get pretty close by combining installable triggers with Gmail’s search function. Set up a time-based trigger that runs every few minutes and searches for emails received since your last check using something like “in:inbox is:unread newer_than:5m”. I store the timestamp of my last run in PropertiesService so I don’t process the same emails twice. This works reliably in my production scripts, though there’s always a small delay based on how often your trigger runs.

yea, totally understand! so, here’s a thought – while Gmail lacks instant triggers, a time-driven trigger is handy. set it to run every minute or so, then use gmailApp.search() with newer_than param to find new mails. not perfect but gets the job done!