Skip to content

How to set up your own mail server

23 mayo, 2021

When we talk about email, the first thing that comes to mind is Gmail, Outlook, Yahoo! or any other mail provider. If we want more privacy, we can move to other services that encrypt and protect our mail. But there is another alternative: setting up your own email server.

Why would anyone want to do this when there are people who manage everything for you for free? There can be several reasons. Maybe you want to have full control of your mail and run some custom filters, you want to know how the mail servers are managed or you do not want anyone other than you to have access to your mail.


Be that as it may, in Genbeta we are going to review today how start setting up your mail server, and we will use the process to explain how email systems work.

We emphasize the start: setting up a mail server is complex, there are many options and we would never finish. At the end of this guide we will have a server from which to send and send emails, and we will mention what the next steps would be if you want to move forward.

You also have to take into account the disadvantages to set up your own server, especially if you intend to use it for your main email account. The maintenance is entirely at your expense. If it falls and stops working, it is very likely that the emails that have been sent to you during that time will stop reaching you. It is also very possible that the most popular providers (Gmail, Outlook) mark your emails as spam to come from an unknown server.

And finally, I want to emphasize the theme of safety. If you really want to use your own server for your main email, you have to worry about system security: user control, secure passwords, no server without authentication, updates … If not, you may find yourself one day with an unpleasant surprise .

Previous steps: a server and a domain name

AWS Console

Congratulations, I see that you are brave and you have not been scared by the possible problems that I have mentioned. Let’s start setting up the server: the first thing we need is, obviously a server.

By server I mean a computer that is always on (if not, no emails will arrive when it is off), be it your home computer, a virtual machine on Amazon or Azure, a Raspberry Pi or whatever. If you don’t have any of that, you can create an account on Amazon Web Services and create a free virtual machine in no time.

In this guide we assume that the server is running Linux. Specifically, I have mounted the server on an Ubuntu 12.04.3 LTS in Azure, although the instructions should not change in other distributions or providers. At most, some configuration file that is elsewhere or packages with another name.

With the server ready, you will need a domain name if you want to receive mail. Any registrar, such as GoDaddy, NameCheap, or Hover, works. When you have the domain, you have to point the MX records to your server. For example, if when creating the server in Amazon it gave you the address ec2-aaa.compute.amazonaws.com, you have to change the MX record of the domain (in the control panel of the registrar) so that its value is that address ec2-aaa.compute.amazonaws.com.

What is this MX record change for? Let’s say you are Gmail and a user tells you to send an email to pepe@pepe.com. Perfect, but there is a problem: which server is pepe.com? Because remember that computers only understand IP addresses, not names. To resolve that name you have to ask the DNS servers, just like when you browse the Internet and the browser resolves the name of genbeta.com. The difference is that Gmail will search MX records, that is, those that refer to mail servers. That is why you have to make sure that the MX record is correct: if not, when they try to send you an email they will not find your server and you will not be able to receive it.

Installing the server: Courier

Courier

Courier installation wizard

There are several mail servers, each with its own options and advantages. In this article we are going to use Courier, which is quite easy to configure, works fine and has not given me any problems.

To install it, we execute the following command (assuming we are in Debian / Ubuntu / similar):

sudo apt-get install courier-mta courier-imap

which will install the server base and the IMAP interface to be able to configure mail clients. A wizard will ask us if we want to prepare the directories for the web interface: we will say yes, although for the moment we are not going to install any web interface.

Once installed, we already have our server running. We only have to change two things: the first is set our name. To do this we execute the following commands as superuser (root):

echo minombrededomino >> /etc/courier/locals
makealiases

With them we will tell the Courier to receive emails destined for mydomainname. Thus, when another email provider (Gmail, to continue with the example above) reaches our server saying “I have an email for you”, Courier will not reject it and the message will be saved.

We will also change a configuration file to put authentication on the SMTP server and thus prevent anyone from using it to send emails. We have to open the file /etc/courier/esmtpd (with superuser permissions) and change two variables: AUTH_REQUIRED for what it’s worth 1 and on the list ESMTP_AUTH we have to add the value "LOGIN". This will be left in the file (in the corresponding lines).

AUTH_REQUIRED=1
ESMTP_AUTH="LOGIN"

We restart the server with sudo courier restart so that it loads the new settings and we can move on to the next step.

Mail client configuration

Now we only need to configure our mail client. If you look at it, we have not configured email accounts, and at the moment Courier only receives emails for user accounts on the server. For example, if your account on the server is Peter (to see the name of your account run whoami), you can only receive emails at the address pedro @ mydomainname.

The connection parameters are as follows:

  • IMAP: port 143, no SSL, with clear password.
  • SMTP– Port 25, no SSL, with clear password.

In both cases, the username and password are those of the server account.

From now on, you will be able to send and receive mail in your account with that own server. Your mail client will connect to port 143 (IMAP) to download the mail, and when it needs to send messages it will do so through your SMTP.

Tasks to do

As I said at the beginning, we have started to set up our mail server. There are still many things to do if you want this server for something more than messing around. Would not be bad improve security of messages with an antispam filter, and SSL connections for SMTP and IMAP ports to prevent your passwords and emails from traveling in plain text over the Internet. You can use self-signed certificates if you want, which are much easier to obtain, but they are not very secure.

I also imagine that you want to have multiple email addresses, or manage different domains from the same server, so you will have to set virtual users in Courier.

If you want to keep messing around, the Courier manual includes all the “basic” administration tasks that you can do with the server. And if something fails, you have the records in /var/log/mail.log to see what happened.

In Genbeta | Five web alternatives to encrypt or protect our emails