We have a test environment with real user data, and during testing, the servers may send emails to these real users. But we definitely don’t want our users to receive those test emails, since they’ll be totally confused and get a bad feeling about our service. Yet we want the emails sent to our company’s domain to be delivered successfully, so we can test with our work email address.
One solution is update the database and change all user emails to dummy ones. However this requires one more step each time the development database is refreshed with real data.
The solution I used is to setup a Postfix SMTP server which only allows several whitelisted domains. The configuration is quite simple.
- Add this into main.cf:
smtpd_recipient_restrictions = check_recipient_access hash:/etc/postfix/recipient_domains, reject
- /etc/postfix/recipient_domains is the whitelist file:
mycompany.com OK anotherdomain.com OK
- Generate hash file:
- Restart postfix service
Now try sending an email to a domain not in the whitelist, like
mail -s "Test email" firstname.lastname@example.org. Look into /var/log/maillog and you will find something like that:
NOQUEUE: reject: RCPT from …: 554 5.7.1 <email@example.com>: Recipient address rejected: Access denied;…
Then on all development servers, configure to use this postfix server as SMTP relay server. This is simple even with sendmail – the “SMART_HOST” option.