You can use /etc/aliases to pipe email directly to a program to process, so if you wanted to run a script to process all email to test@domain.com you would put this line in /etc/aliases (works for postfix, sendmail, etc.):
test: "|/usr/local/bin/processtestemail.php"
Then run "newaliases" to update the database.
Then make sure you have a working program in /usr/local/bin named processtestemail.php.
It can be written in php, bash, perl, python, whatever you want and whatever you have for an interpeter. You could even launch a compiled binary written in c/c++, etc.
There were suggestions for using procmail above, it is a great product, but honestly what I have presented is the fastest and simplest solution and it works in more versions of *NIX with more mailers than any other.
Too, non of the other answers really tell you how to process the inbound message and so you would, in your script read input from standard "in" (stdin) and then parse that data using whatever algorithms you may have to process it properly as follows:
<?php
$fd = fopen('php://stdin','r');
if ($fd) then
{
$email = ''; // initialize buffer
while (!feof ($fd)) // read as long as message
{
$rawemail .= fread($fd,1024); // read up to 1K at a time
ProcessTheMessageChunk($rawEmail);
}
fclose($fd); // done so close file handle
$fd=NULL; // clear file handle
}
else
{
print("ERROR: Could could open stdin...");
};
/*
** Now write your code to fill in the function ProcessMessageChunk()
** and then process the data you have collected altogether using
** that function/subroutine.
*/
?>