Is there any way to trigger a php script when a new email arrives on Google Apps (Gmail)? I've figured out a way to poll the inbox to look for new messages, but I'd ideally like a more event-based solution. Basically I need it to be run from a server somewhere which would parse new email messages and process itself according to the contents of the message.
The Advanced Gmail service allows you to use the Gmail API in Apps Script. Much like Apps Script's built-in Gmail service, this API allows scripts to find and modify threads, messages, and labels in a Gmail mailbox.
Most mail transfer agents let you put in filters that can process incoming mail through a program as it arrives. SpamAssassin works this way, for example.
Because Gmail is hosted, you can't exactly do that.
The only realistic answer in this case is polling. Thankfully Gmail exposes both POP3 and IMAP access, so checking should be very simple. There are a few comprehensive mail reading libraries out there, or you could be insane and poke at IMAP directly.
Edit: Because you're on Google Apps, there's also an Atom feed of unread messages, though you'd still need something to poll the feed, unless you happen to have something handy that will do the polling for you and fire up requests. I vaguely recall something like this existing, but I don't recall what it's called...
It's possible now, using Google Apps Scripts. You have to create a script and attach a trigger to it to run every 1 minute or so. If a new email is found, just invoke your URL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With