I’m working on a Gmail automation project and need to run some code automatically whenever a new message lands in my inbox. I’ve looked into Gmail add-ons documentation but most examples show contextual triggers that only fire when you actually open and view an email message.
What I really want is something that executes immediately upon email arrival, not when the user clicks on it. I found some references to unconditional triggers but those seem to work only after opening the message too.
Is there a way to set up a trigger that responds instantly to incoming mail? I want my script to process emails right when they arrive in the mailbox, before any user interaction happens.
Unfortunately Gmail doesn’t support real-time triggers for incoming emails through Apps Script. The closest solution is using time-driven triggers that run at regular intervals to check for new messages. I’ve been using this approach for about two years now and it works reasonably well despite not being truly instant. You can set up a trigger to run every minute or few minutes, then use GmailApp.search() with date filters to find recently arrived emails. The key is maintaining a timestamp of your last check to avoid processing the same messages repeatedly. While there’s a slight delay compared to instant triggering, most automation tasks work fine with this method. Just be mindful of your quota limits since frequent polling can consume your daily allowance quickly.
Google Apps Script doesn’t offer direct event-based triggers for incoming Gmail messages, which is frustrating but understandable from a security perspective. I’ve dealt with this limitation extensively in my own projects. What works best is combining time-driven triggers with Gmail’s threading and label system. Set your trigger to run every 2-3 minutes and use Gmail’s search operators like “is:unread newer_than:5m” to efficiently identify fresh emails. The trick is implementing proper state management - I store the last processed message ID in PropertiesService to avoid duplicate processing. While not instantaneous, this approach has proven reliable for processing hundreds of emails daily in my automation workflows. The slight delay is usually acceptable for most business use cases, and you can always adjust the trigger frequency based on your specific requirements and quota constraints.
yeah theres no instant trigger for gmail unfortunately. ive been using a workaround with time triggers every 5 mins + gmail filters tho. basically create a filter that auto-labels new emails, then your script searches for that label. way more efficent than scanning the whole inbox everytime and you can prcess stuff pretty quick after arrival.