Pipe incoming email for certain user to external script

Attention

Check out the lightweight on-premises email archiving software developed by iRedMail team: Spider Email Archiver.

This tutorial explains how to pipe incoming email for certain user to an external script (it must be a file on your mail server, not a remote file accessible via ssh/http/ftp/...).

Configure Postfix to use your external script as transport

To pipe incoming emails to external script, you must add your external script as Postfix transport program. Please add below line at the bottom of Postfix config file /etc/postfix/master.cf:

external-pipe   unix    -   n   n   -   -   pipe
    flags=DRhu user=vmail:vmail argv=/path/to/your/external/script.sh

Note:

external-pipe   unix    -   n   n   -   -   pipe
    flags=DRhu user=vmail:vmail argv=/path/to/your/external/script.sh -f ${sender} -d ${user}@${domain} -m ${extension}

Now restart Postfix service to make this new transport available:

# /etc/init.d/postfix restart

Update per-user transport to use this new transport

We need to update per-user transport setting, so that all emails delivered to this user will be piped to this new transport - your script.

* For iRedAdmin-Pro MySQL or PostgreSQL edition, please go to user profile
  page, under tab `Relay`, set `Relay/Transport setting` to `external-pipe`.
sql> USE vmail;
sql> UPDATE mailbox SET transport='external-pipe' WHERE username='user@domain.ltd';

That's all.