Email for multiple virtual domains with exim4 dovecot

This is a Tutorial about setting up exim4 on Debian

Many mail servers need to handle mail for multiple virtual domains. This can easily be achieved with Sendmail, postfix, or qmail.
Here we’ll cover how it’s done with exim4 – the default mailserver for Sarge.

Installing Exim4 – if you need to

Because I’m going to want to scan incoming mail for viruses as they are delivered, rather than filter them out myself once I’ve accepted them I will install the heavy version of the server:

 

Once this has downloaded you will start the debconf configuration process.
The first question asked is ‘Do you wish to use a single monolithic configuration file, or split files?’ Here I chose the split file approach, to make future upgrades more simple.

Once that was done you’re left to choose the type of configuration you wish to use – I chose “an internet site”, because I want to send and receive mail directly. This choice does affect the subsequent questions you’ll have.
Choosing the mail name should be obvious, as should choosing which addresses you wish the server to listen upon.
Once that’s done the basic setup is complete – but we’ve still got to handle the virtual domain setup.

Generate Local Server-side Certificate
Run the command:

 

(adding the ‘–force’ option if you have already done this once. This is a debian specific operation, and there is no analogous step needed on other systems
Then execute:

 

Dovecot
Here comes the command to create a Dovecot certificate:

 

put this only

 

disable this line

 

Handling Virtual Domains
If you’ve just installed the server and you followed the debconf setup I did then you’ll have a server which will send and receive mail for a single domain.
The way that I’ve chosen to handle virtual domain mails, (which was used in the exim3 article) is to have a text file for each domain which contains a mapping between the email address and a local user.
For example assume you wish to handle mail for three domains:
example.net
example.org
example.com
Create three text files:

Each file will contain lines of the form:

Where “address” is the part of the email address to the left of the domain name, and “username” is the account name on the local system which should recieve that mail.
To make it more clear assume:
Alice gets all mail sent to example.net.
Bob gets all mail sent to example.org.
Eve has a mail account at example.com, as does Trent.
This gives /etc/exim4/virtual/example.com:

 

For Bob who has a “catchall” address setup for example.org –

 

And similarly for Alice – /etc/exim4/virtual/example.net:

 

You can also drop all mail for a user by using:

Or to generate a bounce with some text added so that the sender will know why they got it:

Now we just need to make exim4 read these files to know what to do with the incoming mail, a simple enough job.
First of all we need to update the list of domains we handle mail for by editing the file /etc/exim4/conf.d/main/01_exim4-config_listmacrosdefs, change the current “local_domains” with this one:

This will cause the local_domains setting to include the domain list from the directory we’ve specified. (Which is why the text files are named after the domain name to which they apply).
Now that the list of domains has been updated we need to create a new file – edit the file /etc/exim4/conf.d/router/350_exim4-config_vdom_aliases and add the following contents to it:

 

Here we tell exim how to lookup the local recipients.
Once this has been done you can restart the server and see how it works: