Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger PHP Script on New Email (Google Apps / Gmail)

Tags:

php

email

events

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.

like image 958
ggutenberg Avatar asked Jul 12 '10 18:07

ggutenberg


People also ask

Does Gmail support scripting?

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.


2 Answers

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...

like image 96
Charles Avatar answered Oct 22 '22 08:10

Charles


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.

like image 20
Gaurav Gupta Avatar answered Oct 22 '22 06:10

Gaurav Gupta