Hosting your own Mailserver

30/12/2023

Last christmas I decided that it was time to stop using Gigahost as my mail host. That decision had been long coming because I wasn’t very pleased with their performance, mails could take hours to arrive and when my mails also started to get blocked because they missing security configurations on their end I decided it was time to take action.

A few times over the years I have been looking at alternative hosting providers such as Proton Mail and Exchange Online. I like Proton mails offering, but I dont like paying 10 USD per month for an email service, as their 4 USD one custom domain plan doesn’t really cut it for me. Exchange online is an obvious alternative, starting at 4 USD, but I have quite a problem with everything on the internet getting more and more centralized, and with that in mind, the last thing I should support is Microsoft getting another customer.

So as a challenge and learning execise I decide why not just host my own mail server, I mean how hard can it be, everyone did so in the past, and yes there are few more security features regarding mail servers nowadays, but surely it should be doable to a tech savy person. I know a lot of people don’t recommend that you host mail servers yourself, as you are prone to being attacked and being used for sending spam, so I needed to find a software stack that was battle tested and used by others.

Luckily there are options out there, I decided to go with Mail-in-a-box. Mail-in-a-box is a fairly long lived project dating back to 2014, seeing regular release with new features and security patches. It is also not overly complex, but it does wants to own the Ubuntu box that it is installed on (but that is probably a good thing, you dont want more attack vectors on your mail server than neccessary).

With the software chosen I was ready to setup my new server, but then a new problem surfaced. No hosters wants to host email servers anymore, so it is really difficult to find a hosting provider that you can use. Microsoft e.g. in Azure just blocks everything with regards to the SMTP protocol, so you can forget about using them. The same goes for a lot of ISPs, so if you where thinking about hosting your mail server at home think again. I looked around for hosts that people could recommend for mail-in-a-box, and eventually settled with Hetzner as they are a European company, with a good reputation and good prices. However in order to unblock port 25 on their servers you have to own the server for a least 3 months before their support wants to open the port, pretty long wait, but also quite understandable that they dont want people to buy a 5 USD server send spam, and ruin the IPs they own, at least you have to dedicate 15 USD, and a lot of time now to do it.

The server I purchased was CX11 (1 vCPU, 2 GB and 20 GB disk) as it was the cheapers option and seemed good enough for the job. Ordering the server was quick and mostly painless. The problems I encountered doing setup was that they wouldn’t accept my email, so had to signup using a gmail account, and also their payment provider wouldnt accept my VISA card so ended up paying with PayPal. But it has been working fine for the last 12 months so once payment is setup, it is works.

I will not go through how to setup mail-in-a-box I essentially just followed their guide, with the exception that I didn’t want the box to be my DNS server (that is still cloudflare), so I had to setup quite a few DNS names manually for it to work, but mail-in-a-box has a nice overview pages that valides your configuration, that will show if you have misconfigured anything. Once everything was setup, I basically waited 3 months before I contacted Hetzner support to get port 25 open. And then I switched my MX record away from Gigahost.

I didn’t do any fancy migration of the emails, I just copied all emails and folders that I wanted to save directly in the Outlook client from the old Gigahost mailbox to the new Hetzner mailbox using the IMAP for both providers.

Now the setup has been running for 9 months, and I haven’t had any problems with mail-in-a-box it does SSL renewal and auto-patches when needed. It even includes an option to do a off-site backup, which I have setup to copy the files to a cloudflare S3 compatible bucket. I did cause some trouble for myself when I decided to play with the nginx configuration on the box, as I wanted to use the box for temporarily hosting other things. But mail-in-a-box didn’t like my changes to the nginx config, so the mail-in-a-box scripts would mess up things when they run, so eventually I had to give up on using the box for other things.

Bottom line if you are not affraid of fiddling with things it is possible in 2023 to run your own mail server for a price that is comparable with the real providers, and I promise you will learn something in the process. If you are worried that it is going to require a huge time investment, then I estimate that I have spend 8-16 hours on getting this up and running, which I think is a worthwhile investment in taking control and learning more email servers.