Today I will be writing about how to set up your own email server.

You will need the following build for your server

  • Linux OS – Ubuntu 14.04
  • Apache 2
  • MySQL Database
  • PHP 5
  • Root Access
This guide shows you how to set up a secure mail server with Postfix, Dovecot, and MySQL.
By the end of this tutorial, you’ll know how to create mail box for your users, to send and receive email for your domains.

Mail Components

Postfix for Mail Transfer Agent, its the controller of mail server, that decide if a particular user can send email, and how to relay email.

Dovecot – It handle the protocol of IMAP and POP3, allow users to login and check for their mail. It also help save mail files on your server, and the authorization. It query MYSQL database to check for user email and password before allow a mail to be send or view.

MYSQL – The database server to store mail particulars such as domain, aliases, and user accounts

Step 1 – Connect to your server

After logged in, Log in as the root user by entering the following command:


Key in the password for the root user.

Next, you need to install this required library

sudo apt-get install postfix postfix-mysql dovecot-core dovecot-imapd dovecot-pop3d dovecot-lmtpd dovecot-mysql

You’ll be prompted to select a Postfix configuration. Select Internet Site, as shown below.

Selection: Internet Site

You’ll be prompted to enter a System mail name, as shown below. For our example we use, you can use any hostname you want (we will setup this on the next stage, just think of a name you want first)

Setup Host Name

Now type the following code below, for this tutorial I use nano editor, you can use vim if you want to.

nano /etc/hostname

The value should match the hostname you use earlier for postfix.

Next we edit the host records using the command below

nano /etc/hosts

MySQL Database Setup

Now we begin create the necessary tables & records for mail server.

First we login to mysql database

mysql -u root -pYOURSQLPASSWORD

Take note that everyone mysql root password is different, you have to change the value of YOURSQLPASSWORD to your mysql password.

You will see the screen above upon mysql login

Next we will create the database, using the command below

create database mailserver

Next we will create a mysql user and grant the permission to access the database

CREATE USER ‘demouser’@’localhost’ IDENTIFIED BY ‘pass.1234′;

Next we will create the table for domain

CREATE TABLE `virtual_domains` ( `id` int(11) NOT NULL auto_increment, `name` varchar(50) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

We will also create the table for email users

CREATE TABLE `virtual_users` ( `id` int(11) NOT NULL auto_increment, `domain_id` int(11) NOT NULL, `password` varchar(106) NOT NULL, `email` varchar(100) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `email` (`email`), FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

And also the aliases list – email forwarding

CREATE TABLE `virtual_aliases` ( `id` int(11) NOT NULL auto_increment, `domain_id` int(11) NOT NULL, `source` varchar(100) NOT NULL, `destination` varchar(100) NOT NULL, PRIMARY KEY (`id`), FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Next we will begin adding the data, do change the value to suit your domain & user.

INSERT INTO `mailserver`.`virtual_domains` (`id` ,`name`) VALUES (‘1′, ‘’), (‘2′, ‘’), (‘3′, ‘hostname’), (‘4′, ‘’);

Next we create the email login details

INSERT INTO `mailserver`.`virtual_users` (`id`, `domain_id`, `password` , `email`) VALUES (‘1′, ‘1’, ENCRYPT(‘firstpassword’, CONCAT(‘$6$’, SUBSTRING(SHA(RAND()), -16))), ‘’), (‘2′, ‘1’, ENCRYPT(‘secondpassword’, CONCAT(‘$6$’, SUBSTRING(SHA(RAND()), -16))), ‘’);

For this example above, we created the email user with the login password “ firstpassword” and also the other account with password secondpassword

If you need to setup an email forwarding (email alias), you can do the following below [ Optional Step ]

INSERT INTO `mailserver`.`virtual_aliases` (`id`, `domain_id`, `source`, `destination`) VALUES (‘1′, ‘1’, ‘’, ‘’);

PostFix Setup

Open the postfix conf file

nano /etc/postfix/

Comment out the TLS parameters part ,your postfix should look same as the top

Copy and paste the following values into the config file below the TLS settings. This will ease the restrictions and allow users to send email from their home or office. By default, only users who are logged into the server locally are able to send email.

They will be required to log in with a password before able to send email.

The smtpd_sasl_type and smtpd_sasl_path lines tell Postfix to use Dovecot for user authentication. Dovecot already authenticates users checking their email, so it makes sense to have it handle outgoing authentication too.

Next, you would need to change the following mydestination to localhost

mydestination = localhost

And also change the virtual transport value

virtual_transport = lmtp:unix:private/dovecot-lmtp

Make sure your setting is as followed below

Save the changes.

Next, we will create a file for the virtual domain records

nano /etc/postfix/

The mailuser and mailpass need change to the value of the MYSQL user and password that you create earlier on.

Save the change.

We test the postmap response, it should return 1. If not, its mean your setting went wrong.

postmap -q mysql:/etc/postfix/

Next we edit the mailbox mapping config file

nano /etc/postfix/

Save the changes

Test Postfix to verify that it can find the first email address in your MySQL table. Enter the following command

postmap -q mysql:/etc/postfix/

Next edit the alias config file

nano /etc/postfix/

Text postfix to verify it can find your first alias on the MYSQL table.

postmap -q mysql:/etc/postfix/

We will edit the master config file

nano /etc/postfix/

Locate and uncomment the two lines starting with submission and smtps. This will allow you to send mail securely on ports 587 and 465

Save the changes you’ve made to the /etc/postfix/ file.

Do a restart of the postfix service

sudo service postfix restart


Now we begin the setup of Dovecot. Dovecot allows users to log in and check their email using POP3 and IMAP.

nano /etc/dovecot/dovecot.conf

Add the following line to /etc/dovecot/dovecot.conf so Dovecot knows to support IMAP, POP3, and LMTP.

Save your changes. We will begin edit the next config file

nano /etc/dovecot/conf.d/10-mail.conf

Uncomment mail_location line.

mail_location = maildir:/var/mail/vhosts/%d/%n

Find mail_priviledged_group and uncomment it, change it value

mail_privileged_group = mail

Save your changes, and next we create your mail folder.

mkdir -p /var/mail/vhosts/

We create a user and add it to group 4000

groupadd -g 4000 vmail
useradd -g vmail -u 4000 vmail -d /var/mail

We change the permission of the folder of its owner to user vmail

chown -R vmail:vmail /var/mail

Next we edit the file /etc/dovecot/conf.d/10-auth.conf

nano /etc/dovecot/conf.d/10-auth.conf

We disable plaintext auth by uncomment this line

disable_plaintext_auth = yes

Change the auth mechanism

auth_mechanisms = plain login

Add a Hash Tag / Comment on the line below

#!include auth-system.conf.ext

Next we uncomment auth-sql.conf.ext and comment the rest as shown below.

Save your changes.

Next, we edit another config file

nano /etc/dovecot/conf.d/auth-sql.conf.ext

Paste the content below.

Save the changes and edit another config file

nano /etc/dovecot/dovecot-sql.conf.ext

Uncomment and set the value as mysql for driver

driver = mysql

Set the database connect line, change the mailuser and mailpass value to your database login credentials.

connect = host= dbname=mailserver user=mailuser password=mailpass

Uncomment the default pass scheme and change its value as below

default_pass_scheme = SHA512-CRYPT

Uncomment password query line and set as below

password_query = SELECT email as user, password FROM virtual_users WHERE email=’%u';

Save your changes.

Next change the permission of the folder

chown -R vmail:dovecot /etc/dovecot
chmod -R o-rwx /etc/dovecot

We will edit the socket information

nano /etc/dovecot/conf.d/10-master.conf

Save your changes and open the dovecot ssl config file next.

nano /etc/dovecot/conf.d/10-ssl.conf

Save your changes & restart dovecot service.

sudo service dovecot restart
You are done with the setup, next you can proceed to use mail agent like iMail(MAC) or Thunderbird(Window), Outlook Express, Squirrel Mail(Webmail – Server Based) to setup your mail with the server. If you would like to engage Website Design Hub Pte. Ltd. to setup for your email server, you can call us directly at the phone 8288 3810. Administration Charges applies.

Download Mozilla – ThunderBird