Setting up a catch-all e-mail account on Linux using Postfix

Want to help support this blog? Try out Oh Dear, the best all-in-one monitoring tool for your entire website, co-founded by me (the guy that wrote this blogpost). Start with a 10-day trial, no strings attached.

We offer uptime monitoring, SSL checks, broken links checking, performance & cronjob monitoring, branded status pages & so much more. Try us out today!

Profile image of Mattias Geniar

Mattias Geniar, February 20, 2012

Follow me on Twitter as @mattiasgeniar

You may want to install a catch-all e-mail account on a Linux server for catching the bounced mails from a mailing for instance. Each unique return-path can then be used to map it to a bounced e-mail and unsubscribe that user from your mailing. Using Postfix, that’s a simple thing.

Check if no other MTA is running

You probably don’t want any other mailserver running besides postfix. So for safety, only continue if the command below does nothing.

# telnet localhost 25
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused

Please note, Linux systems by default will run a sendmail or Exim on localhost to send e-mails. If you’re sure you can stop that mailservice, use the init.d scripts to stop it.

Install postfix

Use your trusty package manager.

# yum install postfix
# chkconfig postfix on

Configure your catch-all

Create a /etc/postfix/virtual file with the following content.

# grep -vP "^#" /etc/postfix/virtual
@my.server.tld              catchall

You can replace the @my.server.tld with the actual domain you want to have the catch-all for. You can simply add more lines in that file with additional domains if you need them.

Then, run the postmap command to conver the text file to a hashmap that Postfix understands.

# postmap /etc/postfix/virtual

Edit your Postfix’s main.cf file and make sure the following line is present.

# grep -P 'virtual_alias_maps' /etc/postfix/main.cf
virtual_alias_maps = hash:/etc/postfix/virtual

And restart your Postfix to make the changes active.

# service postfix restart

Testing it

You should be able to telnet to your server and send a test mail to a dummy account.

# telnet my.server.tld 25
Trying 127.0.0.1...
Connected to servername (1.2.3.4).
Escape character is '^]'.
220 servername ESMTP Postfix
HELO server.tld
250 servername
mail from: <test@test.be>
250 2.1.0 Ok
rcpt to: <randomdata1234@my.server.tld>
<u>250 2.1.5 Ok</u>
^]
telnet> quit
Connection closed.

The line shown in underline is the answer you would look for, where the server accepts the random user you’ve just sent an e-mail to. And there, you’ve just set up a catchall e-mail address in Postfix. Seeing as you’ll receive loads of spam as well, you want to make sure you clean that mailbox regularly. ;-)



Want to subscribe to the cron.weekly newsletter?

I write a weekly-ish newsletter on Linux, open source & webdevelopment called cron.weekly.

It features the latest news, guides & tutorials and new open source projects. You can sign up via email below.

No spam. Just some good, practical Linux & open source content.