I first started running my own dedicated server a number of years ago. At the time I was not particularly impressed with the control panel software available (Plesk, cPanel) as it seemed to somewhat take over the server and then meant that if you had any custom requirements there was always the extra task of checking if they were possible to do with the control panel / how to do them in a way that was compatible with the control panel. Also there was the extra cost of licensing the control panel software. I therefore decided to configure the server from scratch.
This post discusses setting up SMTP services using the Exim mail server software in a virtual hosting environment with support for:
- Multiple domains
- Multiple accounts (mailboxes)
- Catch all accounts
- Authenticated SMTP relaying
- Forwarding to multiple addresses (lists)
- Forwarding of messages to system users to external accounts
- Bouncing email with/without a custom message
- Blackhole’ing email
- Using Postini spam filtering (blocking email sent directly to the server)
The server was setup such that there were several “customers” who each had their own user account on the system. Mail for accounts is stored in users home directories under “/mail”. Each customer can have one or more domains.
Exim is very powerful however unfortunately that power comes at the cost of complexity in terms of setting things up. Its not just a case of point and click, or adding a line to a text file for new accounts, you first have to actually setup the rules in Exim that tell it (e.g. in the case of accounts) to accept messages from domains hosted on the server in the ACL, “route” that mail to a custom “transport”, configure the transport to (depending on recipients address and domain) lookup where to store the new mail on the server.
After a reasonable amount of time spent both reading the Exim documentation and searching on the Internet I was able to come up with a configuration supporting the above features centred around the following text files / directories:
- “/etc/exim/authrelay” – A list of usernames/passwords allowed to relay mail.
- “/etc/exim/userdomains” – A list associating domains back to user accounts on the system.
- “/etc/exim/domains” – A directory containing files for each domain name that themselves contain a list of mailbox accounts for that domain.
- “/etc/exim/virtual” – A directory containing files for each domain name that themselves contain a list of forwards (including catch all, bounce, blackhole) for that domain.
- “/etc/exim/postini_filtered” – A list of domains where mail should only be accepted for delivery from the Postini spam filtering service.
- “/etc/exim/userforward” – A list of local system user accounts for which mail should be forwarded to alternate accounts to cater for system services such as cron which by default send results of commands run to e.g. useracctname@localhost.
- Download the /etc/exim/exim.conf file, change the “primary_hostname” to be your servers hostname, change the email address listed under “redirectrootmail” to be your email address and copy it to your server.
- Setup the configuration files / directories:
chmod 640 /etc/exim/authrelay
chown root:mail /etc/exim/authrelay
chmod 640 /etc/exim/userdomains
chown root:mail /etc/exim/userdomains
chmod 750 /etc/exim/domains
chown root:mail /etc/exim/domains
chmod 750 /etc/exim/virtual
chown root:mail /etc/exim/virtual
chmod 640 /etc/exim/postini_filtered
chown root:mail /etc/exim/postini_filtered
chmod 750 /etc/exim/userforward
chown root:mail /etc/exim/userforward
Add SMTP Relay Accounts
echo smtprelayuser: smtprelaypass >> /etc/exim/authrelay
echo example.com: localusername >> /etc/exim/userdomains
chmod 640 /etc/exim/domains/example.com
chown root:mail /etc/exim/domains/example.com
chmod 640 /etc/exim/virtual/example.com
chown root:mail /etc/exim/virtual/example.com
Add Mailbox Accounts
echo emailacctname >> /etc/exim/domains/example.com
echo emailtoforward: firstname.lastname@example.org >> /etc/exim/virtual/example.com
Add Forwards To Multiple Users
echo emailtoforward: email@example.com, firstname.lastname@example.org >> /etc/exim/virtual/example.com
Forward Mail Sent Directly To Local Users (e.g. by cronjob)
chmod 640 /etc/exim/userforward/username
chown root:mail /etc/exim/userforward/username
echo email@example.com > /etc/exim/userforward/username
Add Catchall For Domain
echo *: firstname.lastname@example.org >> /etc/exim/virtual/example.com
Bounce Mail To An Account
echo emailacctname: :fail: >> /etc/exim/virtual/example.com
Bounce Mail To An Account With A Custom Message
echo emailacctname: :fail: Gone away, no forwarding address >> /etc/exim/virtual/example.com
Blackhole Message Sent To An Account
echo emailacctname: :blackhole: >> /etc/exim/virtual/example.com