Geoff Garbers

Husband. Programmer. Tinkerer.

Send all email to a single recipient using Postfix

Jun 24, 2014

When in development, there will more than likely be a few instances where you will need to send email to clients or users. However, the last thing you want to do is mistakenly send an email to a client or user while in development.

With this in mind (and the fact that I use a development virtual machine which wouldn’t ever need to send emails to anyone but myself), I set up postfix to send all outgoing email to a single email address (mine) - regardless of who it is addressed to. Using the steps outlined below, you’ll be able to get your Postfix installation set up in a similar fashion.

Installation steps

In /etc/postfix/main.cf, append the following to the end of the configuration file:

# Custom addition - only send to one email address, ever.
recipient_canonical_classes = envelope_recipient
recipient_canonical_maps = regexp:/etc/postfix/recipient_canonical_map

In /etc/postfix/master.cf:

# Find the line that looks similar to this (without the leading #):
# smtp      unix  -       -       n       -       -       smtp

# Add this line immediately afterwards:
        -o smtp_header_checks=regexp:/etc/postfix/smtp_header_checks

Create /etc/postfix/recipient_canonical_map and place the following into it:

# Enter the email address to which you want all emails delivered.
# For example, my configuration reads "/./ geoff+dev@garbers.co.za"
/./ your@email.here

Create /etc/postfix/smtp_header_checks and place the following into it:

# This will basically take the whole subject line, and place "[devmail] " in front of it - providing another way of filtering it.
/Subject:(.*)/ REPLACE Subject: [devmail] $1

Once you have updated these configuration files, and created these files, give postfix a reload by executing the following:

[~]# service postfix reload

If you have the mail command installed, send a test to non-existent email address:

[~]# echo 'test' | mail -s 'test email' noone@example.org

If all your configuration has been updated and changed successfully, you should receive an email addressed to noone@example.org in your your@email.here inbox. Now, no matter who an email is sent to, you can be certain it won’t be sent to any clients or other unintended recipients.