How to Redirect All Postfix Emails to a Single External Email Address for Testing Environments


2 views

When setting up a test email server with Postfix, you might need to intercept and redirect all outgoing emails to a single external email address (like Gmail) for debugging or testing purposes. This is different from local redirection as it requires proper SMTP routing.

The most reliable method is using Postfix's transport_maps feature combined with sender_canonical maps. Here's how to implement it:


# Main.cf modifications
sender_canonical_maps = regexp:/etc/postfix/sender_canonical
transport_maps = regexp:/etc/postfix/transport_redirect

# /etc/postfix/sender_canonical
/.*/    testuser@gmail.com

# /etc/postfix/transport_redirect
/.*/    smtp:[smtp.gmail.com]:587

For Gmail specifically, you'll need to add SASL authentication:


# /etc/postfix/sasl_passwd
[smtp.gmail.com]:587    testuser@gmail.com:yourpassword

# Set permissions and postmap
chmod 600 /etc/postfix/sasl_passwd
postmap /etc/postfix/sasl_passwd

If you don't need to preserve sender information, configure Postfix as a null client:


# /etc/postfix/main.cf
default_transport = smtp:[smtp.gmail.com]:587
local_transport = error:No local delivery
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous

After making changes, always test your configuration:


postmap /etc/postfix/sender_canonical
postmap /etc/postfix/transport_redirect
postfix reload
echo "Test email" | mail -s "Test Subject" anyaddress@example.com
  • Consider using an App Password if using Gmail with 2FA enabled
  • For production systems, implement proper logging to track redirections
  • Be aware of Gmail's sending limits (500 emails/day for free accounts)
  • For AWS SES or other providers, adjust the SMTP settings accordingly

When setting up a test email server, you might need to redirect all outgoing emails to a single external address (like Gmail) instead of delivering them to their original recipients. This is particularly useful for development and testing environments where you want to monitor all outgoing messages without spamming real users.

The most effective way to achieve this is by modifying Postfix's transport maps. Here's how to implement it:

# In /etc/postfix/main.cf:
transport_maps = hash:/etc/postfix/transport

Then create/edit /etc/postfix/transport:

# Redirect ALL emails to your external address
*       smtp:[smtp.gmail.com]:587

You'll also need to configure SMTP authentication if required by your external provider:

# In /etc/postfix/main.cf:
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = encrypt

Another approach is to configure Postfix to always BCC all messages to your test address:

# In /etc/postfix/main.cf:
always_bcc = email@gmail.com

This method keeps the original recipients but sends a copy to your test address.

If you need more granular control, you can specify different redirects based on domains:

# In /etc/postfix/transport:
example.com       smtp:[smtp.gmail.com]:587
*.example.org     smtp:[smtp.gmail.com]:587

After making changes, remember to:

postmap /etc/postfix/transport
systemctl reload postfix

Then send a test email and verify it arrives at your external address.

When redirecting to external services:

  • Use proper authentication
  • Consider rate limits of the external provider
  • Be aware of potential privacy implications