Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?


share|improve this question
up vote 3 down vote accepted

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.


share|improve this answer
Whis is it! Write a lightweight daemon that stays connected to Gmail through IMAP, and checks for new messages every 10secs. The daemon exec() the script I need to run. Great! Any tips to the lightweight IMAP daemon-client? – Trident Splash Jul 1 '09 at 12:52

If you control your web server: You can use gmail to forward the emails to the web server and then use a pipe in your /etc/aliases file to feed the emails to a php script which can read them from standard in. (Or the equivalent mechanism to pipe emails to a script in whatever email server (MTA) you choose, I personally use exim.) Basically, your aliases file would look like

username: |/var/www/myphpscript.php

and myphpscript.php would read the email on STDIN and act on it.

If you don't control your web server: IMAP supports an IDLE command which allows a client to a connection to the server open while waiting for an email to arrive. (This is somewhat like a PUSH notification, except that the client must establish and keep a open connection to the server) You could write a script which would open a connection to the gmail IMAP server and use the IDLE command to wait for a new email. This would require the ability to run a persistent process on the server. There may be a way to use something like fetchmail to do this and then pass the email to a php script.

Hope that helps!

share|improve this answer

If you were hosting your own mail server you could use something like procmail. I don't think there is an option to automate the handling of email.

share|improve this answer

If you've got administrative rights on the mail server, why not write a daemon/perl/shell/php script to check for the existence of the file, then sleep 5 seconds (or however short an interval you care about), then check again. That's all your "push" mail server will do, and you'll be eliminating a layer of complexity.


Sorry, I misunderstood. I thought you controlled the pop server.

Do you know what software the pop server is running? That would go a long way in learning whether it supports some sort of push mechanism.


Check this out:

It supports "NOOP", to prevent an idle connection from timing out. Sounds like just what you need to implement a PHP POP3 client.

share|improve this answer

You can't push POP, it's a strictly come-and-get-it protocol.

Are you able to use your own mail server instead of GMail? Failing that, does GMail allow for auto-forwarding email? In either case you can set up a mail server which can then trigger actions based on that incoming mail.

share|improve this answer

My answer is probably more programming related then sysadmin...

I believe it may be possible via the Google API to write a script that starts a long-lived http request that will basically just wait around until email is available.

Most 'push' email is client initiated. A client will make a HTTP request asking for new mail, and the server will send them an answer when there is.

I suspect, that if wait times of 60 seconds are too long for you, then you may want to reconsider your decision to use gmail. Gmail is pretty good for normal usage, but immediate delivery of messages doesn't seem to be something they focus their system on doing. Google doesn't offer many guarantees about speedy delivering messages.

If you run your own server, then their are many options to configure the mail server to deliver the message to a processes. Check the docs for that mail server.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.