There are very good reasons to want your own e-mail server. I’ve tried several setups and found the Courier-mailserver the simplest to work with. Here’s a tutorial on how to set it up.
This should take about 15 - 30 minutes.
Here’s what you will have in the end:
- Own email server where you can configure an unlimited number of email addresses with an unlimited number of domain-names. However, in this setup each address is bound to a user on the Linux system.
- SMTP and IMAP with encryption. Even though email may travel unencrypted to it’s destination, the link between your mail client and your mail server should be encrypted.
Here’s what you need:
- An Ubuntu 13.10 server in the cloud (other Ubuntu version should work). You can get those for 5 euros a month or so. (Because a virtual server is just fine for this task. You don’t need 32 Gigs of RAM and 8 cores..)
- A domain-name where you can configure DNS. Specifically: You want to change the MX-record.
- Some basic knowledge of Linux.
Ok, I will explain to you now how to install the Courier mail suite. I know there are several popular other ones, but this just happens to be a guide about Courier.
If you get stuck or have special config needs, then please also refer to the end of the article where I list a few helpful links.
First, connect to your email-server using SSH. Make sure you can execute commands with sudo. Start with
to update your packet sources.
telnet is in there for testing. You can remove it if you you want, but telnet is nice to check if SMTP works and what its configuration is.
Configuration - Part 1
Enter your domain in /etc/courier/locals. Do the same in /etc/courier/esmtpacceptmailfor.dir/domains.
Run sudo makeacceptmailfor to have these changes accepted.
Create a user, let’s say “suntke” (I think that’s a Frisian first name..).
And create a password for him.
This user can login to your linux-box now using the password you specified. Now, let’s create a maildir for him.
(prefix with sudo as needed. The -q flag sets a quota. Just use some high number..)
So, let’s say the domain-name was “example.com”, then this means you have just created the email-address “firstname.lastname@example.org”. Incoming email will be stored in /home/suntke/Maildir.
You can add as many users and domains as you like.
You can now go ahead and send and receive emails. However, I suggest you add some extra security through encryption.
Configuration - Part 2
In /etc/courier/esmtpd-ssl set AUTH_REQUIRED to 1. This forces authentication. In /etc/courier/esmtpd set ESMTPAUTH=”LOGIN PLAIN”.
/etc/courier/esmtpd.pem should exist and be not world-readable. This should automatically enable ESMTP STARTTLS, encrypted SMTP.
When you are done, you can restart everything: (added newlines for readabilty)
Configuration - Part 3, Client-side, Incoming
So, this was the server-side. Let’s go ahead and configure a mail-client. I will jsut go ahead and use the fictional user/address email@example.com here, as well.
Here’s the settings in Mail on OSX. It’s pretty much analogous with Thunderbird etc.
For Accounts/Account Information:
Configuration - Part 4, Client-side, Outgoing
Configuration - Part 5, DNS
Use your domain registrar’s console (or do it yourself) to set the MX record. In this case you’d just set example.com, because you didn’t configure any subdomains such as mail.example.com.
Your Mail-program or OS will probably complain about the certificates not being signed. You can decide if you want to have them signed. It doesn’t make much sense if it’s just for yourself. Mark the certificate as “trusted” or add them to your “trusted certs” collection in your keychain.
If it doesn’t work
Try restarting both the server processas as well as your mail-client. Make sure your system trusts the certificates. Double-check your passwords.
As I was trying to verify this tutorial on a testing server it wouldn’t work at first, but restarting the client and accepting the certs permanently did the trick.
That’s it, congratulations. You have your own email-webserver. Here’s a few ideas on what you could do next:
- install spamassassin. It’s not difficult and I’m sure you can google it. Or I can write a short post about it in the future.
- install GPGTools so you can encrypt your email end-to-end. (this is for Mac, similar software is available for every major OS though.)
- install squirrel-webmail if you need/want a web-frontend. Not my kind of thing but I thought I’d mention it.
- use a database to manage users.
- write filters for courier to automate things.
- install the whole thing on a tiny computer like the Raspberry Pi, use a dynamic-DNS vendor that support updating of MX records, configure your router to forward mail to your Pi, and host the whole thing at home. ;)
Here are some links that might help you with Courier:
Comments, suggestions, bugs?
If you have something to add or correct, please let me know so I can update this guide. Let’s spare others extra work and frustration..
Update (April 16th, 2014)
In case you mess up along the way and then get errors like “Mailbox unavailable” (but you have already solved the underlying problem), you can reset a mailbox like this:
I needed this recently when I could not figure out what the problem is. Turns out, I solved the problem earlier but didn’t clear the “broken mailbox flag”.
P.S.: You can follow me on Twitter.