Creating a mail server with Postfix, Dovecot & Squirrelmail

One of the most widely used services around the world is EMail. Whether its any organization or individual, email is used by everyone. Anyone can create an Email account with ease on any of the email servers like gmail, outlook etc but for an organization, it makes sense to use a private email server as it can be designed with needs of the organization, organizational policies (security or otherwise) can be placed that might not be available in public email servers.
In this tutorial, we are going to learn to create a email server. But before we proceed, let’s check out some terms associated with Email servers.

Terminology

Mail Transfer agent or MTA – is responsible for transferring of mail from sending mail server to receiving mail server. Sendmail & Postfix are examples of MTA. We will be using Postfix for the purpose of this tutorial.

Postfix is free & open source mail transfer agent (MTA) that routes & delivers Email. It is fast, secure & easy to administer and is a great alternative to SendMail MTA.

Mail User Agent or MUA– interacts with end user directly. Email Web Interface, MS Outlook or thunderbird are some examples of MUA. We will be using SquirrelMail for configuring a web interface for our email server.

Squirrelmail provides a graphical interface for sending mail. As you can understand sending & receiving mail via command line can be bit of a hassle. With the help of Squirrelmail we can open a console in our web browser for sending & receiving mail.

Protocols- Protocols like IMAP or POP3 are used to fetch mail from recipient mail server. We will be using Dovecot to accomplish the same.
Dovecot is an IMAP and POP3 mail server for Linux. It provides a way for Mail User Agent (MUAs) to access their mail. Its created to be fast, secure , requires very less administration & uses very less memory to work.

Pre-requisites for mail server

1- Remove SendMail

There can only be one MTA on a server & since we will be using POSTFIX, we need to remove Sendmail from the mail server. To remove previous installation of sendmail, run

$ yum remove sendmail

2- Setup a static IP

We need a static IP for our mail server since dynamically assigned IP will not work. So either reserve a IP on DHCP server, if being used or assign an IP address to mail server manually.

3- Setup a hostname

Also we need a permanent hostname for our server. To change hostname, run

$ hostnamectl set-hostname mail.unxschl.com

4- A MX record on the DNS server

We also need to create a MX record for our mail server on DNS server. A MX record points IP address of the mail server to its FQDN & vice-versa.

5- An entry in /etc/host

Open /etc/hosts file & create an entry for our hostname

$ vi /etc/hosts

192.168.1.120 mail.unxschl.com

6- Enable epel-repository

We will need to install epel-repository for installing SquirrelMail on our server. To install epel-repository for

RHEL/CentOS 7

$ rpm -Uvh https://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-8.noarch.rpm

RHEL/CentOS 6 (64 Bit)

$ rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

RHEL/CentOS 6 (32 Bit)

$ rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm

Step 1 – Installing Postfix

To install postfix, run

$ yum install postfix

Once the Postfix has been installed, we will move onto configure it,

Step 2 – Configuring Postfix

Path for main configuration file for Postfix is ‘/etc/postfix/main.cf’. Open the file & make changes to it as follows,

$ vi /etc/postfix/main.cf

myhostname = mail.unxschl.com # Line 77
mydomain = unxschl.com # Line 85
myorigin = $mydomain # Line 101
inet_interfaces = all # Line 115
inet_protocols = all # Line 121
#mydestination = $myhostname, localhost.$mydomain, localhost, # Line 166 (comment it)
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain # Line 167 (uncomment)
mynetworks = 192.168.1.0/24, 127.0.0.0/8 # Line 266
home_mailbox = Maildir/ # Line 421

Once the changes have been made to the file, save & exit the file. And restart the postfix service to implement the changes,

$ systemctl restart postfix
$ systemctl enable postfix

Configurations for postfix are complete, next we will test postfix .

Step 3 – Testing Postfix

Create a user to test the postfix setup. So add a new user & assign it a password,

$ useradd susan
$ passwd susan

& enter the password. Next to test the postfix setup, we will telnet into our our localhost smtp

$ telnet localhost smtp

& you will see a screen with following,

Trying ::1…
Connected to localhost.
Escape character is ‘^]’.
220 mail.unxschl.com ESMTP Postfix

Now to connect to you mail server, type

Trying ::1…
Connected to localhost.
Escape character is ‘^]’.
220 mail.unxschl.com ESMTP Postfix

To send a mail, type your user name with the following command

mail from:<susan>
250 2.1.0 Ok

Then enter recipient mail address

rcpt to:<susan>
250 2.1.5 Ok

& enter the content of the mail

BODY OF THE MAIL #this is the Email body
354 End data with <CR><LF>.<CR><LF>
This is a test mail .
.

After you mail body is complete, type . (dot)

250 2.0.0 Ok: queued as E2B522032F93
To exit from the session, type
quit
221 2.0.0 Bye
Connection closed by foreign host.

Now, check if the user has received any mail or not,
Goto user’s default mail directory for new mails which is /home/susan/Maildir/new. Next list the directory items in the folder

$ ls

& we should see an item something like ‘2456127891.Btd845931Grs445986.mail.unxschl.com’, that’s the mail that was sent by user ‘susan’. To read it, we will use ‘cat’ command.
So, our Postfix is working fine & we will move to configuring Dovecot.

Step 4 Installing & configuring Dovecot

To install Dovecot, use the following command

$ yum install dovecot

Next we will configure dovecot, to do that open its configuration file i.e. ‘/etc/dovecot/dovecot.conf’,

$ vi /etc/dovecot/dovecot.conf

& uncomment the line 24, which is

protocols = imap pop3 lmtp

next open the file ‘/etc/dovecot/conf.d/10-mail.conf’ & again uncomment line 24,

$ vi /etc/dovecot/conf.d/10-mail.conf
mail_location = maildir:~/Maildir

next, open the file ‘/etc/dovecot/conf.d/10-auth.conf’ & make changes as following

$ vi /etc/dovecot/conf.d/10-auth.conf

disable_plaintext_auth = yes          # line 10 – Uncomment it
auth_mechanisms = plain login     # line 100—edit it

& then open ‘/etc/dovecot/conf.d/10-master.conf’ & uncomment then add ‘postfix’ to it,

$ vi /etc/dovecot/conf.d/10-master.conf

mode = 0600
user = postfix
group = postfix
[…]

Now restart dovecot service to implement all the changes we made,

$ systemctl restart dovecot
$ systemctl enable dovecot

Step 5 Testing Dovecot

In order to test dovecot, we will again log into telnet session with POP3

$ telnet localhost POP3
Trying ::1…
Connected to localhost.
Escape character is ‘^]’.
+OK Dovecot ready.
Enter user & password,
user susan
+OK
pass *****
+OK Logged in.

To view mail, type

retr 1
+OK 415 octets
Return‐Path: <susan@unxschl.com>
X‐Original‐To: susan
Delivered‐To: susan@unxschl.com
Received: from localhost (localhost [IPv6:::1])
by mail.unxschl.com (Postfix) with ESMTP id B84519637Z29
for <sk>; Thur, 30 Mar 2017 9:52:31 +0530 (IST)
Message‐Id: 2456127891.Btd845931Grs445986.mail.unxschl.com
Date: Thur, 30 Mar 2017 9:52:31 +0530 (IST)
From: susan@unxschl.com
This is a test mail .

To quit,

Quit
+OK Logging out.
Connection closed by foreign host.

Last thing to do after configuring the postfix & dovecot server is to install the GUI for our mail server, which we will do by installing Squirrelmail.

Step 6 Installing & configuring Squirrelmail,
We have already enabled epel repository, which will provide package for installing squirrelmail. To install squirrelmail, run

$ yum install squirrelmail

To configure squirrelmail , we need to run a script named ‘conf.pl’ located at ‘/usr/share/squirrelmail/config/’

$ cd /usr/share/squirrelmail/config/
$ ./conf.pl

We will now be presented with a configuration wizard with the following options,

SquirrelMail Configuration : Read: config.php (1.4.0)
———————————————————
Main Menu —
Organization Preferences
Server Settings
Folder Defaults
General Options
Themes
Address Books
Message of the Day (MOTD)
Plugins
Database
Languages
Set pre-defined settings for specific IMAP servers
C Turn color off
S Save data
Q Quit

Now press ‘1’ to select ‘Organisation Preferences’ & make changes to organization name, again by selecting ‘1’,

SquirrelMail Configuration : Read: config.php (1.4.0)
———————————————————
Organization Preferences
Organization Name : SquirrelMail
Organization Logo : ../images/sm_logo.png
Org. Logo Width/Height : (308/111)
Organization Title : SquirrelMail $version
Signout Page :
Top Frame : _top
Provider link : http://squirrelmail.org/
Provider name : SquirrelMail
R Return to Main Menu
C Turn color off
S Save data
Q Quit

Set your organization name & press ‘enter’.

Similarly change other settings as well, once done save all the settings by pressing ‘s’. Return to first menu on the configuration wizard & now change the ‘Server settings ’ by pressing ‘2’,

SquirrelMail Configuration : Read: config.php (1.4.0)
———————————————————
Server Settings
General
——-
Domain : localhost
Invert Time : false
Sendmail or SMTP : Sendmail
Update IMAP Settings : localhost:143 (uw)
Change Sendmail Config : /usr/sbin/sendmail
R Return to Main Menu
C Turn color off
S Save data
Q Quit

Change your domain name & save it.

SquirrelMail Configuration : Read: config.php (1.4.0)
———————————————————
Server Settings
General
——-
Domain : unxschl.com
Invert Time : false
Sendmail or SMTP : SMTP
Update IMAP Settings : localhost:143 (uw)
Change Sendmail Config : /usr/sbin/sendmail
R Return to Main Menu
C Turn color off
S Save data
Q Quit

After the configurations have been made, exit the wizard by typing ‘Q’.
Next we need to create a apache host settings for Squirrelmail in ‘/etc/httpd/conf/httpd.conf’ . So open httpd.conf & make an entry of the following at the end of the file,

$ vi /etc/httpd/conf/httpd.conf

Alias /webmail /usr/share/squirrelmail
<Directory /usr/share/squirrelmail>
Options Indexes FollowSymLinks
RewriteEngine On
AllowOverride All
DirectoryIndex index.php
Order allow,deny
Allow from all
</Directory>

 

Save file & restart httpd services for changes to take effect.

$ systemctl restart httpd

Step 7 Accessing the Webmail

To access web interface for email server, enter the following URL in the web-browser,

http://IPaddress/webmail

Then enter your username password & once the user has been authenticated, access to webmail will be granted. We can read our mail, compose new mails through webmail.
We now have a fully functional local mail server. But if we need to send mail over internet, then we need to configure the email server with a PUBLIC IP address & need to create a MX record for your email server onto our ISP’s DNS server.

Shujat Husain

A continuous learner with several years of experience in linux environment. I love to read, learn & write about Linux & new technologies. And I also write at linuxtechlab.com

What is in your mind, about this post ? Leave a Reply

Close
  Our next learning article is ready, subscribe it in your email

What is your Learning Goal for Next Six Months ? Talk to us