Q: How can I run an arbitrary Perl or shell script for all or selected incoming mail?
A: Install Procmail. Read the manual pages (there are several). thank you, you are.
: 0 * conditions, if any | OWN script here
Conditions in their simplest form are regular expressions to match the header of each incoming mail message. Correction: Even easier, you can completely exclude condition lines if you want to do your action (in this case, run the shell script) unconditionally.
More difficult conditions can also be exit codes from another shell scripts or programs or tests against the entire message body, or against Procmail variables (Procmail variables are also exported to the subprocess environment, therefore they are essentially environment variables. This is described in detail in this FAQ. )
Actions can also be to save the message to a folder (added to the Unix mailbox file or written to a new file in a directory) or forward the message to one or more other addresses. Finally, an action can be a nested block of more “recipes,” as these conditional comparisons are called in the Procmail jargon to try if the external state is fulfilled. There is a complete scoop on the procmailrc (5) manual page.
Obviously, you are not limited to Perl or shell scripts. You can start something from a Unix command line that can be launched from Procmail, in principle, although running interactive programs usually does not make much sense.