How to Run a script when a mail arrives in mail server? (Debian)

Looks like someone else has already answered this but thought I'd put down a specific answer for you.

I would use procmail and use a recipe in your .procmailrc similar to this:

#turn this off when you're finished testing :)
VERBOSE=on
LOGFILE=/home/user/procmail.log

:0 c #the c means continue on after this recipe is parsed
| /path/to/your/script

You'll also need a default recipe at the bottom to direct the mail into your maildir.


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 [email protected] 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.
*/

?>

Thanks SA Experts for Hire: we do it all big or small tekops.com


Here's a good howto on incoming mail processing. The simplese thing to do is to use the .forward mechanism as described, to pipe a message through a script. Create a mode 600 .forward file in the user's home directory and put a pipe to a script in it:

"|$HOME/bin/your.program -and some arguments" 

However, you should look at using procmail instead, as that howto details. Procmail gives you a lot of advantages, such as more sophisticated logging and mail processing. Here's a simple .procmailrc example (again from that same howto):

:0
* !^FROM_DAEMON
* !^FROM_MAILER
* !^X-Loop: [email protected]
| $HOME/bin/my.script 

which has some nice features, like the ability to detect and stop mail loops.