How to route email to a script

Solution 1:

Obviously will work Douglas Land's answer, with a little addition.
Suppose you have only localhost.localdomain at $mydestination in main.cf.
Then add a virtual alias :

[email protected] -> [email protected]

In main.cf you will need:

alias_maps=hash:/etc/aliases

Then add to /etc/aliases :

somelocaluser: "|/your/script/here"

newaliases+postfix reload and you are good to go.

Solution 2:

I'd go with Procmail. We use this to parse all sorts of mail out to worker scripts and IRC bots for notifications.

Here is an example of how to handle email. http://linuxgazette.net/issue14/procmail.html

Relevant short example done here for ease:

  1. Create a local user called handler, for example.

  2. Go to handler's home directory and create a file called .procmailrc containing:

:0
| $HOME/bin/my.script

This will pass the entire inbound email to the script $HOME/bin/my.script through STDIN.

Also here is where I got help with Procmail when I was first trying to pass email to a script: How to use procmail to get a message into a variable

Solution 3:

You can pipe an entry to a script via /etc/aliases, RE:

foo: "|/your/script/here"

modify /etc/aliases then run newaliases and reload the postfix process.