views:

257

answers:

4

We've built a web service that needs to check constantly for email messages. Basically, an user sends us an email, and the server should make actions based on that email. We could use a crontab PHP script that checks for new messages every minute, with POP. But that's kind of offensive to the popserver and not very efficient (1min is too long).

But, I've read about PUSH email using IMAP around mobile devices. In my case is not a mobile device but a webserver.

Can I push an email to my webserver and have it execute a PHP script? We're using GMail as POP/SMTP/IMAP server.

EDIT 1 from the answers, we figured out:

  1. there must be a 24/7 running process (daemon) on my webserver checking for emails

  2. this daemon may communicate with Gmail using: i) POP with NOOP coomand or ii) IMAP with IDLE command

What's the best? POP or IMAP? Google seems to invite more the use of IMAP.

I don't want to overuse gmail (what's their 'fair use' for checking email? every 10secs?

+1  A: 

If you want to use postfix as your MTA you can alias an email address to any executable like this. It really isn't that difficult to setup on a *nix box...

Jason Punyon
We won't install any mailsoftware this time. Thanks.
sopppas
+1  A: 

I've no experience with IMAP, but had to do the same thing. If I were you, I'd install Postfix and use the pipe command (basically lets you run an arbitrary script) to invoke a small http client that parses the email and sends it to your server.

You can replace postfix with whatever MTA you'd like, of course. The point is: don't implement your own email server. Use an existing one and use its hooks to send the mail off to where ever it is you want, however it is you want.

Richard Levasseur
+1  A: 

email_scheduler.c

int main()
{
        while(1){
                        sleep(5); // every 5 secs the email_parser.php would get executed.
                        system("/usr/bin/php -q /var/www/html/email_parser.php");
        }
}

email_parser.php :

/* Use any email message parser to parse the email messages like MIME message parser based on your purpose : Refer : http://www.phpclasses.org/browse/package/3169.html */

Also check relevant answers here for more info

Webrsk
A: 

There's an old (but great) article on Evolt about piping emails to a php script, which therefore gets the full content of the email and can act on it.

http://www.evolt.org/article/Incoming_Mail_and_PHP/18/27914/index.html

adam