Before we start we will have to make a few clarifications. For the installation and the configurations below we use two domain names, one for the mail server:
which will be used to access the mail server (mail boxes, configurations of the mail server, etc.) and one for the domain name:
which will be used for the email accounts (for example: email@example.com).
It is important to understand that the mail server must have a domain name which corresponds to it (in our case this is mail.example.com) but it can have multiple domain names for the email accounts (like: firstname.lastname@example.org, email@example.com, firstname.lastname@example.org, etc.). In our case, we will use only one domain name for the email accounts.
Feel free to change them to your own domains or sub domains, but keep in mind something very important, all domains must be registered and verified, otherwise you take the risk your server or IP address to be blocked.
Also, the domain name mail.example.com used for the mail server must be configured to point to the IP address of the machine which is going to be used as a mail server and where you will install "EasyMail". Example configuration:
All commands below must be run as root or another user with root privileges.
Below are listed the dependencies which will be installed automatically during the installation of “EasyMail”:
If any of those services is already installed on your machine, "EasyMail" will stop the installation. You must be sure that you have cleaned up your machine properly before you start. The reason for this is very simple, different services have different versions and dependencies. Starting from scratch is the best way to be sure that the installation will go well and you won't enter some "dependency hell".
There are cases when we may want to build the mail server on Docker. To do so, follow the steps below.
Install Docker if you still don't have it on your machine. For further information check Docker installation.
We will use a docker image of Ubuntu 14.04. Go to the terminal of your machine and execute the following command (read below first):
Further explanations are required. The command above will build a new container (named “easymail”) with a new fresh installation of Ubuntu 14.04 and mapping for the following ports: 110, 25, 995, 80, 443, 587, 993 and 143.
For the proper work of the mail server, it is important each of those ports to be freed and not occupied by the services running on your physical machine. Each of them is used for:
If any of the ports above is already occupied you will have to use another one or to free them. For example, if you have a web services installed on your physical machine, you most probably use ports 80 and 443, so you will have to use different ports in order to finish the installation above. For example you can use 8080 instead of 80 and 44380 instead of 443. The command above will change:
During the installation described below we will use ports 443 and 80 but bear in mind that the ports may vary depending on your particular case.
As you may have noticed, during the installation of the container we have mapped the directory: /etc/ssl/certs/. This is the directory which contains the SSL certificates for the domain name of the mail server - mail.example.com. We can use the SSL certificates to encrypt the communication with the mail server or we can skip this option. This is entirely an optional step.
When the creation of the container completes, you will be let inside the Docker container. Continue with the installation of “EasyMail”.
Execute the following:
Download “EasyMail” and start the script install.sh:
During the installation you will be asked a few questions. Below you will find answers which are applicable only for the current example.
The admin's email address mentioned during the installation is email@example.com.
For the installation of your own SSL certificate, if you choose “y”, you will be asked two additional questions addressed to the SSL certificates (this step requires the ssl directory to be mapped during the creation of the docker container):
After the configuration of the SSL certificates:
Now be patient. The installation will take a few minutes.
In the end of the installation of "EasyMail", the installation script will generate data for the DKIM record (you will need this later) and a few random passwords for the MySQL database (used to store email accounts, configurations, etc.). The DKIM record and the MySQL passwords will be displayed on the screen of your terminal when the installation ends. Write them down because you will need them!
If you have missed to write down your DKIM record, use these commands to find it:
In this directory you will find another sub-directory corresponding to your domain. Go inside:
Print the content of mail.txt
Copy the content. This is your DKIM record.
If you have missed to write down the passwords for the MySQL database, follow the additional steps below in order to restart them:
When you finish the installation, "EasyMail" will automatically start all services (MySQL, Nginx, php5-fpm, Postfix, Dovecot, Spamassassin and OpenDKIM).
The next important step is to add MX, CNAME and SPF records and to finish the configuration of DKIM for the domain name used for the email accounts (in our case this is example.com). The SPF record and DKIM are optional but advisable.
The configuration of the CNAME is required if we want to configure our email address with a Desktop mail client. Steps:
With the help of these CNAMES, the mail server is able to generate the following XMLs:
They are used by the desktop mail clients for automatic configuration with your mail server. You can also use the information within the XML for other manual setups.
Please take into account that we can access the XML documents through port 80 but this port may vary depending on the configurations of your web service.
It is up to you, how you will set up the SPF record but for the sake of clarity we will present a short example.
If you want to know more about how to add SPF records and the different configuration options just google for "add SPF record" or read this tutorial SPF Record.
Everyone who receives an email from an email account with domain name example.com (for example: firstname.lastname@example.org) can check the SPF record of this domain and to see that it authorizes only the server with IP address recorded in the DNS A record of the mail server mail.example.com, to send emails. If somebody else tries to send emails on behalf of example.com from a different server, this server most probably will go to the spam filters and permanently blocked.
Go to the domain register of your domain names and for the domain name used for your email accounts add the following TXT record (DKIM record):
Example (do not use the example key below, it's only an illustration and will not work on your server):
During the installation of "EasyMail" you have been asked to provide the path to the SSL certificates. If you want to change the certificates, simply replace them and restart your web service.
“EasyMail” comes with a nice mail client “RoundCube”. In order to access it go to your web browser and type the following:
Of course, you will need an email account to log in. Continue reading for further information how to add email accounts.
When you access your email account with RoundCube, you can change your password from “Settings” -> “Password”.
All email accounts are stored in the MySQL database. To add a new email account you will have to access the MySQL database (remember, you will need the MySQL password). We will do that through the terminal.
The database used by “EasyMail” is “mailserver”.
You will find four tables:
The name of the tables are chosen in accordance with Postfix standards for email management. Better understanding of Postfix email management will give you more clear idea of how to work with these tables. The table “virtual_users” contains all email accounts. We can list them:
or we can add a new record.
Please bear in mind that the password field is encrypted with openssl_encrypt enctyption.
"EasyMail" is configured to start all required services automatically if the machine reboots. This will save you some time because you don't have to start the services by yourself. We will review how this works.
The file contains the following code:
These are the services which will be starter automatically if the machine reboots. That's all you need to know, "EasyMail" takes care for the rest.
If you want to forward all incomming emails to another email:
If you want to forward emails to Gmail, another option is to use Gmail SMTP client.
The good thing about the current mail server is that it can be used to send transactional emails. For the configuration with an external client we can use:
Example for Laravel: