views:

96

answers:

3

Is there a way to configure a server or a script to execute a php script when an email is received?

In theory this could be extended to other protocols, such as XMPP or SMS, etc.

What I have in mind is a user could send a message to [email protected] and this would trigger a script which would then do whatever needed to be done, either irrelevant to the message (an automated message that gets sent whenever some other even occurs, like a server having issues) or related to the message (like it could store the subject in a database that other users could view as an RSS feed).

I know that most list-serve software have a means of sending commands (like unsubscribe), but I'm not sure how complicated the process is and if it is feasible to have something like this on a server-script level.

Would this need to occur at the IMAP/SMTP level, or could it be done closer to the script or HTTP server?

Just to give some context, one of the things I'm considering is a message-based clock in server for one of my work sites. Users could IM/email/text that they are at their scheduled location and this could trigger a script that would update a DB, rather then send the managers a direct message they need to log. This would just be one option (a web-based method is already in the works), but we want to make it as platform/protocol independent as possible. But if we can achieve that, we can look at implementing it for many other day-to-day needs.

Another way of asking might be: is there a way to have "discovery" of users from a server-script app or does something need to be doing a constant check to relevant sources to see such changes?

+3  A: 

Use a cron job to check for emails through a pop3 interface. If an "interesting" mail is found, run your php script.

Note that the cron script can be in PHP too.

jldupont
Since my project is one of many within a directory that is itself on a server shared by several other departments, I always look for the most low-key methods. Thus my instinct was to avoid a daemon or cron job that would be a constantly running process. But with two answers pushing this direction, I am now wondering if I am being too conservative or if I simply have to face it that there isn't a realistic way of setting up something that only runs when triggered by an external source (such as an email or SMS).
Anthony
+3  A: 

If you control your own machine you can configure it to pipe the e-mail through a filter. Simplest would be to setup a new account and setup a .forward or alias

The following examples are for Sendmail, but (all?) Unix e-mail programs offer a similar service.

Setting up an alias (option 1)

Look in the directory /etc on your server for your alias file. Add the line:

script: "|/path/toyourscript/pipe.php"

Using a .forward file (option 2)

Create a .forward file in your main home directory.

"|/path/toyourscript/pipe.php"

or:

[email protected],"|/path/toyourscript/pipe.php"

If you are on shared hosting then most hosting providers also provide the possiblity to "pipe" e-mails received to a particular account through a script instead of storing them in a mailbox. Check the CPanel setup.

Martijn Dijksterhuis
I don't have control over the machine, but I do have the ability to create a forward file for any messages that come to my user account `[email protected]`. So I this will probably be the best way to go or at least what I had in mind. If push comes to shove I can request more accounts that go to the same directory that I can forward once I have demonstrated that the concept works and is worth it. I can always have another prettier email forward to my user account and have that initiate the script. Thanks for the idea!
Anthony
+1  A: 

As jldupont said, it is easy to do ith php itself, simple reading the smtp continuosly with a cronjob.

Otherwise, you could use a daemon, in python for example.

Both ways allow you to do an important thing: check if the sender of the email is a your user (you have the users in a db, right?), or a spambot (nomatter what kind of anti-spam you use, soon or later, [email protected] will be full of spam)

DaNieL
php itself is also a perfect candidate for writing a daemon.
stroop
+1 for the spam reminder. That is a nightmare I had totally overlooked.
Anthony