exim4 with wildcard email aliases

This post is more than 7 years old.

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:     [email protected]
www-data: [email protected]
site1:    [email protected]
site2:    [email protected]
site3:    [email protected]
...

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:

/etc/exim4/exim4.conf.template

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:

lsearch{/etc/email-addresses}

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: [email protected]
*:    [email protected]

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.