Php – Push email to a apache/php server

apache-2.2gmailimapPHPsmtp

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 or ii) IMAP with IDLE

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?

thanks!

Best Answer

If you use POP3 and keep the connection open, you may not get new messages -- I don't recall if it's part of the spec or not, but the POP3 servers I've dealt with essentially lock the mailbox for the duration of the POP3 session, so no new mail will (appear to) arrive as long as the POP3 session is open (STAT and UIDL and LIST and LAST -always- returned the same response until you QUIT and re-login).

If you use IMAP, you should be able to keep the IMAP connection open and just poll it periodically for new mail. This is much cheaper than logging in, checking, and disconnecting if you want to do it every (say) 10 seconds.

I would not run the poller in the front end webserver. I would have a backend long-running process (daemon) that polls for changes, and communicate via some message passing system to the PHP app in the frontend webserver. ("message passing system" could be as simple as writing request & status information to a table in a shared database).

You can write the poller daemon in PHP if you prefer. You could get extra-fancy with this process & have it adapt to a changing mailboxes: if the users mailbox changes constantly, then stay connected and poll frequently. If the mailbox doesn't change, then disconnect the IMAP connection and check it again 5 minutes later.

James

Related Topic