Question: I want to execute a backup script (.sh) when I receive a new mail on my mailserver (running on Debian with Postfix / Dovecot). I've found many posts on this topic, but no one give me a working solution. Here the last solution I have tested : <https://stackoverflow.com/questions/27230324/run-script-when-postfix-dovecot-get-new-mail>. Does anyone have a suggestion ?

EDIT: When I receive a new mail, I want to run a bash script AND let postfix/dovecot put this new mail in the ~/Maildir folder. I'm looking for a kind of "middleware" that will not block the standard process

Answer: 
If your actual question is on how to retain a backup of every message received (and sent) by your mailserver, the answer is to set the option [**`always_bcc`**](http://www.postfix.org/postconf.5.html) and specify a mailbox. 

Depending on your setup you can forward messages received by that mailbox (or alias) to a script for further processing. 

For instance in [/etc/aliases](http://www.postfix.org/aliases.5.html):

```
 mailbox: "| /usr/local/bin/script.pl"

```

**Edit based on comments**: 

Quite old school, but when your email users are system users and have home directories, there is the old sendmail style `.forward` file, still [supported by postfix](http://www.postfix.org/local.8.html) as well. The syntax is a bit arcane, but a `~mailuser/.forward` wiith the following contents will both deliver the message to the users mailstore and forward it to a script:

```
\mailbox, "| /usr/local/bin/script.pl"

```
May be, you need [postfix milter](http://www.postfix.org/MILTER_README.html)?

> 
> You specify SMTP-only Milter applications (there can be more than one) with the smtpd\_milters parameter. Each Milter application is identified by the name of its listening socket; other Milter configuration options will be discussed in later sections. Milter applications are applied in the order as specified, and the first Milter application that rejects a command will override the responses from other Milter applications.
> 
> 
> 

```
/etc/postfix/main.cf:
    # Milters for mail that arrives via the smtpd(8) server.
    # See below for socket address syntax.
    smtpd_milters = inet:localhost:portnumber ...other filters...

```

The general syntax for listening sockets is as follows:

```
unix:pathname

    Connect to the local UNIX-domain server that is bound to the specified pathname. If the smtpd(8) or cleanup(8) process runs chrooted, an absolute pathname is interpreted relative to the Postfix queue directory.
inet:host:port

    Connect to the specified TCP port on the specified local or remote host. The host and port can be specified in numeric or symbolic form.

    NOTE: Postfix syntax differs from Milter syntax which has the form inet:port@host.

```

Use the `unix:pathname`