exim4 with wildcard email aliases

When you configure exim4 as your email transport, you will likely need to tell it to replace some user names with aliases. To make your life simpler, use wildcards to do the job for you.

I just spent some time setting up a new web server on a DigitalOcean droplet, making sure that each site runs in php-fpm under its own user to keep them isolated from each other. I also set up a backup task that runs as its own user. Each of these users need to be aliased to my webmaster email account, so that emails won’t be dropped for having an invalid email address. As I add new sites, I must ensure that their php-fpm accounts also get aliased to webmaster.

On previous servers, I’d just manually edit /etc/email-addresses and add users as required. That can get tedious, and it’s also easy to forget — and have emails go missing!

root:     bofh@example.com
www-data: webmaster@example.com
site1:    webmaster@example.com
site2:    webmaster@example.com
site3:    webmaster@example.com

There must be a better way! 😃

Well, there is a better way. It is actually possible to turn on wildcards for the email aliases file in exim4. You just need to edit the exim4.conf.template file in the exim4 configuration folder and change two lsearch directives to wildlsearch directives. First, locate the config file: on Debian, Ubuntu, and derivatives, you should find it at:


Edit that file with your favourite editor (vim) — you’ll need root access, so sudo if you need to. Then search for email-addresses to find the two lines you need to edit (they’re close together, don’t worry). Both lines will have pieces that look like this:


Change lsearch to wildlsearch and save the file.

Now you can edit /etc/email-addresses and simplify it to use wildcards. Processing happens from top to bottom, so you can put specific aliases at the top and leave the wildcard rules until last.

root: bofh@example.com
*:    webmaster@example.com

I’m using a very simple wildcard rule because I just want a catch-all that will replace any unknown user with the webmaster email address. If you need to get a bit more fancy than that, check out the exim4 documentation for wildlsearch syntax examples.

NB: once you’re done with editing those files, don’t forget to restart the exim4 service! e.g.

service exim4 restart

And the job is done for the new sites I added today, and any I might add in the future.