Postfix – Catch-All, Pipe to Script

Postfix is a powerful, open-source MTA for sending mail.

Piping a specific email address to a script or forward the emails to another address is easy but creating a catch all that pipes requests to a script is a little more complicated. Let’s jump right into it.

What we’re going to do is pipe all incoming emails on a specific domain that don’t have mailboxes defined to a script so that we can capture the input and process the data.

For the purpose of this article, let’s consider the domain mydomain.tld

1. Create a Catch-All Alias

Let’s create a catch-all for the domain which is a virtual alias. Create/edit the virtual_aliases file of Postfix.

vi /etc/postfix/virtual_aliases

Add the following entry to the file:

@mydomain.tld   localuser@mydomain.tld

localuser should be a valid, existing user on the system with privileges to access the script that we are going to pipe to. Usually this would be the user of the domain, virtual host and the directory where the script resides.

2. Create a New Postfix Transport Service

Create/edit the transport configuration file:

vi /etc/postfix/transport

Add the following entry to it to define the domain and the transport service name:

mydomain.tld    mytransportname:

You can add a username after the colon but it is not necessary.

3. Postmap to Recompile Postfix DNS

Run the following commands to compile the new virtual alias and transport into the Postfix DNS:

postmap /etc/postfix/virtual_aliases

And then:

postmap /etc/postfix/transport

4. Add Transport to Postfix Master Configuration

We need to define the new transport service in the master.cf of Postfix.

vi /etc/postfix/master.cf

Add this entry at the very bottom of the file:

mytransportname   unix  -       n       n       -       -       pipe
  flags=FR user=localuser argv=/path/to/my/script.php
  ${nexthop} ${user}

5. Define Virtual Aliases and Transport in Postfix

Edit the Postfix main.cf file to ensure the virtual aliases and transport configuration is loaded. Just check – these lines may already exist in the configuration so you might need to modify them. If they don’t exist, add them at the bottom of the file.

vi /etc/postfix/main.cf

These are the two entries needed:

transport_maps = hash:/etc/postfix/transport
virtual_alias_maps = hash:/etc/postfix/virtual_aliases

Done. Restart Postfix

That’s it, we’re done. That should do the trick. One last thing, just restart the Postfix service:

service postfix restart

Be the first to reply

Leave a Reply

Your email address will not be published. Required fields are marked *