In this tutorial we are going to setup a local mail server with Postfix, Dovecot & SquirrelMail. But before we begin installation, let’s learn in brief about mail server.

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.

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.

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.

Pre-requisites

  • Remove SendMail (if installed),

To remove previous installation of sendmail, run

$ yum remove sendmail

This is required to remove any conflicts between sendmail & postfix.

  • Setup a static IP

A DHCP assigned IP will not work at all, we require a static IP. So make sure we have one, if using DHCP server, make sure to reserve an IP address for the mail server. Refer to our tutorial on DHCP server to learn how to reserve IP address.

  • Setup a hostname

We will also need a permanent hostname for our server. To change hostname, run

$ hostnamectl set-hostname mail.ltechlab.com

You can also read out tutorial which details 5 ways you can change your system hostname.

  • A working DNS server

To use our mail server, we need a working DNS server with mx record entry for our mail server. To create a new DNS server & adding mx records for mail server, refer our tutorial on DNS server

  • An entry in /etc/host

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

$ vi /etc/hosts
192.168.1.120     mail.ltechlab.com

  • Enable epel-repository

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

RHEL/CentOS 7

$ rpm -Uvh https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-11.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

We will install Postfix using  yum,

$ yum install postfix

Postfix is now installed & we will now configure it,

 

Step 2 – Configuring Postfix

Main configuration file for Postfix is ‘/etc/postfix/main.cf’. open the file to make changes to it,

$ vi /etc/postfix/main.cf

myhostname = mail.ltechlab.com                           # Line 77
mydomain = ltechlab.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)
ynetworks = 192.168.1.0/24, 127.0.0.0/8            # Line 266
home_mailbox = Maildir/                                            # Line 421

Make sure you make changes as per your domain. Save the file & exit 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

To test our Postfix setup, we will need a user. So firstly we will add a new user in our machine & assign it a password,

$ useradd mailuser
$ passwd mailuser

& enter the password of your choosing. Next 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.ltechlab.com ESMTP Postfix

Now to connect to you mail server, type

ehlo localhost
250-mail.ltechlab.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN

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

mail from:<mailuser>
250 2.1.0 Ok

Then enter recipient mail address

rcpt to:<mailuser>
250 2.1.5 Ok

& enter the mail you want to type

data     # will put text in mail 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, let’s check if the user has received any mail or not,

Goto user’s default mail directory for new mails which is /home/dan/Maildir/new. Next list the directory items in the folder

$ ls

& you should see an item something like ‘2456127891.Grd71I393g3e8I235126.mail.ltechlab.com’, that’s the mail that was sent by user ‘mail user’. To read it, you can 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

Once Dovecot is installed, 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

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

$ vi /etc/dovecot/conf.d/10-auth.conf
# line 10 – Uncomment it
disable_plaintext_auth = yes
# line 100—edit it
auth_mechanisms = plain login

& last file to edit is ‘/etc/dovecot/conf.d/10-master.conf’, open it & 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

We will now test dovecot by again logging into our telnet session with POP3

$ telnet localhost POP3
Trying ::1...
Connected to localhost.
Escape character is '^]'.
+OK Dovecot ready.

Enter user & password,

user mailuser
+OK
pass *****
+OK Logged in.

To view mail, type

retr 1
+OK 415 octets
Return‐Path: <mailuser@ltechlab.com>
X‐Original‐To: mailuser
Delivered‐To: mailuser@ltechlab.com
Received: from localhost (localhost [IPv6:::1])
by mail.ltechlab.com  (Postfix) with ESMTP id D34567837Z13
for <sk>; Fri, 17 Mar 2017 2:41:26 +0530 (IST)
Message‐Id: 2456127891.Grd71I393g3e8I235126.mail.ltechlab.com’
Date: Fri, 17 Mar 2017 2:41:26 +0530 (IST)
From: mailuser@ltechlab.com
This is a test mail .

To quit,

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

Our Dovecot is also working fine but as you can see it does not feel right reading mail without GUI, so let’s install GUI for our mail server using Squirrelmail.

 

Step 6 Installing & configuring Squirrelmail,

Install Squirrelmail using yum,

$ 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

It will then open a configuration wizard with the following options,

SquirrelMail Configuration : Read: config.php (1.4.0)

---------------------------------------------------------

Main Menu --

  1. Organization Preferences
  2. Server Settings
  3. Folder Defaults
  4. General Options
  5. Themes
  6. Address Books
  7. Message of the Day (MOTD)
  8. Plugins
  9. Database
  10. Languages
  1. Set pre-defined settings for specific IMAP servers

C Turn color off

S Save data

Q Quit

Select ‘1’ Organisation Preferences & then changes your 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’. You will now return first menu on the configuration wizard, we will now change our ‘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 : ltechlab.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

Our configuration for this wizard are now complete, so exit the wizard by typing ‘Q’. Next  we need to create a apache host settings  for Squirrelmail in ‘/etc/httpd/conf/httpd.conf’  & enter the following to 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 apache service to implement the changes.

$ systemctl restart httpd

 

Step 7 Accessing the Webmail

We can now access our webmail by entering the following URL in our web-browser,

http://IPaddress OR domain name/webmail

Then enter your username password & you can now access your webmail. You can read your mail, compose new mails through webmail. If needed more users, create them as we created our mailuser.

 

That’s it guys, you now have a fully functional local mail server. But if you want to send mail over internet then you need to configure your mail server with a PUBLIC IP & make sure to ask your ISP to create MX record for your mail server.

If having any issues or questions, please feel free to mention them in the comment box below.

 

If you think we have helped you or just want to support us, please consider these :-

Connect to us: Facebook | Twitter | Google Plus

Become a Supporter - Donate us some of you hard earned money: [paypal-donation]

Linux TechLab is thankful for your continued support.