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

I want to parse and insert some information from the mail that just arrived in a mission critical application's mail server.

I there any way to configure my mail server so that it run a script when mail arrives.

Although I have a debian system but any generic solution (Linux) will do.

share|improve this question
possible duplicate of Processing incoming emails with Python – Ignacio Vazquez-Abrams Apr 19 '11 at 7:40
nope no duplicate – dmourati Apr 19 '11 at 8:27
@dmourati: Yes it is. – Ignacio Vazquez-Abrams Apr 19 '11 at 9:41
What if he's not using postfix or python? – dmourati Apr 19 '11 at 22:49
Found my answer @ – Rahul Prasad Apr 27 '11 at 9:15
up vote 6 down vote accepted

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 :)

: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.

share|improve this answer
+1 procmail Edit your .forward and add to taste. Start here – dmourati Apr 19 '11 at 8:26
+1 - the right solution, procmail gives a lot more flexibility than calling your script directly via .forward, although I'd recomend configuring postfix to use procmail as the MDA rather than chaining from the bundled MDA - e.g. – symcbean Apr 19 '11 at 11:38

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):

* !^X-Loop:
| $HOME/bin/my.script 

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

share|improve this answer

In postfix you could hold all incoming messages (that matches some pattern, or simply all of them), have your application pick them up in the hold/ directory and then place them back to the incoming/ directory once done. This is how the antispam filter MailScanner works when used with postfix.

In the Postfix configuration file /etc/postfix/ add this line:
header_checks = regexp:/etc/postfix/header_checks
In the file /etc/postfix/header_checks add this line:
/^Received:/ HOLD

The effect of this is to tell Postfix to move all messages to the HOLD queue, when your application is done I believe you should put the mails in the incoming/ directory.

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.