Setup A Local Mail Server In CentOS 7

Introduction

This tutorial describes how to setup a local mail server using Postfix, Dovecot And Squirrelmail in CentOS 7. Please note that I said “local mail server”. This tutorial doesn’t help you if you want to send or receive mails to outside like Gmail or yahoo. However, if you want send/receive mails to outside, you should configure the mail server with a public IP, and request your ISP to configure the MX record of your mail server in their DNS server.

I tested this setup on CentOS 64 bit server. Although, the same steps should work on RHEL and Scientific Linux 7 distributions. My test box details are given below:

  • OS: CentOS 7 64bit minimal server
  • IP Address: 192.168.1.150/24
  • Hostname: server1.unixmen.local

Let us get started now.

Prerequisites:

1. Remove default MTA sendmail first if it’s already installed. Sendmail will not be installed by default in minimal installation, so you can skip this step.

yum remove sendmail

2. Setup DNS server and add the Mail server MX records in the forward and reverse zone files.

To install and configure DNS server, refer the following link.

And, don’t forget to ask your ISP to point your external static IP to your mail domain.

3. Add hostname entries in /etc/hosts file as shown below:

vi /etc/hosts

Add your FQDN:

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.150 server1.unixmen.local server1

4. I disabled SELinux to reduce the complexity in postfix configuration.

To do that, edit:

vi /etc/sysconfig/selinux

Change SELINUX=enforcing to SELINUX=disabled.

SELINUX=disabled

5. Install EPEL Repository:

Squirrelmail webmail client is not available in CentOS official repositories. So let us enable EPEL repository.

yum install epel-release

6. Allow the Apache default port 80 through your firewall/router:

firewall-cmd --permanent --add-port=80/tcp

Restart firewall using command:

firewall-cmd --reload

Restart your server to take effect all changes.

Install Postfix

Postfix is a free open source mail transfer agent (MTA). It is fast, secure and easy to administer. It’s an alternative to Sendmail, which is the default MTA for RHEL.

Now, install Postifix using command:

yum install postfix

Configuring Postfix

Edit /etc/postfix/main.cf file:

vi /etc/postfix/main.cf

Find and edit the following lines:

## Line no 77 - Uncomment and set your mail server FQDN ##
myhostname = server1.unixmen.local

## Line 85 - Uncomment and Set domain name ##
mydomain = unixmen.local

## Line 101 - Uncomment ##
myorigin = $mydomain

## Line 115 - Uncomment and Set ipv4 ##
inet_interfaces = all

## Line 121 - Change to all ##
inet_protocols = all

## Line 166 - Comment ##
#mydestination = $myhostname, localhost.$mydomain, localhost,

## Line 167 - Uncomment ##
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain

## Line 266 - Uncomment and add IP range ##
mynetworks = 192.168.1.0/24, 127.0.0.0/8

## Line 421 - Uncomment ##
home_mailbox = Maildir/

Save and exit the file.

Start/restart Postfix service now:

systemctl enable postfix
systemctl restart postfix

Testing Postfix mail server

First, create a test user called SK.

useradd sk

Set the password for the user:

passwd sk

Access the server via Telnet and enter the commands manually shown in red colored text.

telnet localhost smtp

Sample output:

Trying ::1...
Connected to localhost.
Escape character is '^]'.
220 server1.unixmen.local ESMTP Postfix
ehlo localhost     ## Type this line ##
250-server1.unixmen.local
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
mail from:<sk>     ## Type this - mail sender address ##
250 2.1.0 Ok
rcpt to:<sk>     ## Type this - mail receiver address ##
250 2.1.5 Ok
data     ## Type this to input body of Email ##
354 End data with <CR><LF>.<CR><LF>
Welcome to unixmen mail server     ## Body of the Email ##
.     ## Type dot (.) after composing your email ##
250 2.0.0 Ok: queued as E2B522032F93
quit     ## Type quit to exit from mail ##
221 2.0.0 Bye
Connection closed by foreign host.

Now navigate to the user sk mail directory and check whether the new mail has been received.

ls /home/sk/Maildir/new/

Sample output:

1437722056.Vfd01I203e3e7M938078.server1.unixmen.local

Success! A new mail is received to the user “sk“.

To read the mail, enter the following command:

cat /home/sk/Maildir/new/1437722056.Vfd01I203e3e7M938078.server1.unixmen.local

Sample output:

Return-Path: <[email protected]>
X-Original-To: sk
Delivered-To: [email protected]
Received: from localhost (localhost [IPv6:::1])
 by server1.unixmen.local (Postfix) with ESMTP id E2B522032F93
 for <sk>; Fri, 24 Jul 2015 12:42:36 +0530 (IST)
Message-Id: <[email protected]>
Date: Fri, 24 Jul 2015 12:42:36 +0530 (IST)
From: [email protected]

Welcome to unixmen mail server

Done. Postfix is working!!

Install Dovecot

Dovecot is an open source IMAP and POP3 mail server for Unix/Linux systems.

To install it, run:

yum install dovecot

Configuring Dovecot

Edit file /etc/dovecot/dovecot.conf file,

vi /etc/dovecot/dovecot.conf

Uncomment the following line:

## Line 24 - umcomment ##
protocols = imap pop3 lmtp

Edit file /etc/dovecot/conf.d/10-mail.conf file

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

Make the changes as shown below:

## Line 24 - uncomment ##
mail_location = maildir:~/Maildir

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

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

And make the changes as shown below:

## line 10 - uncomment##
disable_plaintext_auth = yes

## Line 100 - Add the word: "login" ##
auth_mechanisms = plain login

Edit file /etc/dovecot/conf.d/10-master.conf,

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

Make changes as shown below:

## Line 91, 92 - Uncomment and add "postfix"
#mode = 0600
   user = postfix
   group = postfix
[...]

Start Dovecot service:

systemctl enable dovecot
systemctl start dovecot

Testing Dovecot

It’s time to test Dovecot configuration. Enter the following command in Terminal:

telnet localhost pop3

Enter the commands manually which are marked as bold:

Trying ::1...
Connected to localhost.
Escape character is '^]'.
+OK Dovecot ready.
user sk     ## Enter the mail user name ##
+OK
pass centos     ## Enter the password ##
+OK Logged in.
retr 1     ## Type this command to view mail ##
+OK 415 octets
Return-Path: <[email protected]>
X-Original-To: sk
Delivered-To: [email protected]
Received: from localhost (localhost [IPv6:::1])
 by server1.unixmen.local (Postfix) with ESMTP id E2B522032F93
 for <sk>; Fri, 24 Jul 2015 12:42:36 +0530 (IST)
Message-Id: <[email protected]>
Date: Fri, 24 Jul 2015 12:42:36 +0530 (IST)
From: [email protected]

Welcome to unixmen mail server
.
quit     ## Type 'quit' to exit ##
+OK Logging out.
Connection closed by foreign host.

As you see in the above result, Dovecot is working!

Install Squirrelmail

Sending and receiving mails form command line is not easy all the time. It is better if we do it from a graphical console. No worries. We can easily send/receive mails using webmail client called Squirrelmail via a web browser.

Make sure that you’ve installed and enabled EPEL repository.

Then, Install Squirrelmail using the following command:

yum install squirrelmail

Configuring Squirrelmail

Navigate to /usr/share/squirrelmail/config/ directory:

cd /usr/share/squirrelmail/config/

..and run the following command to configure Squirrelmail.

./conf.pl

The following wizard will open. Enter choice “1” to set your organization details:

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

D. Set pre-defined settings for specific IMAP servers

C Turn color off
S Save data
Q Quit

Command >> 1

The following wizard will open. Enter “1” again to modify your organization details:

SquirrelMail Configuration : Read: config.php (1.4.0)
---------------------------------------------------------
Organization Preferences
1. Organization Name : SquirrelMail
2. Organization Logo : ../images/sm_logo.png
3. Org. Logo Width/Height : (308/111)
4. Organization Title : SquirrelMail $version
5. Signout Page : 
6. Top Frame : _top
7. Provider link : http://squirrelmail.org/
8. Provider name : SquirrelMail

R Return to Main Menu
C Turn color off
S Save data
Q Quit

Command >> 1

Set your Organization name and press Enter:

We have tried to make the name SquirrelMail as transparent as
possible. If you set up an organization name, most places where
SquirrelMail would take credit will be credited to your organization.

If your Organization Name includes a '$', please precede it with a \. 
Other '$' will be considered the beginning of a variable that
must be defined before the $org_name is printed.
$version, for example, is included by default, and will print the
string representing the current SquirrelMail version.

[SquirrelMail]: Unixmen

Similarly, set all the details such as organization title, logo, provider name in the above wizard. Once you done, press “S” to save the changes, and press “R” to return back to your main menu:

SquirrelMail Configuration : Read: config.php (1.4.0)
---------------------------------------------------------
Organization Preferences
1.  Organization Name      : Unixmen
2.  Organization Logo      : ../images/sm_logo.png
3.  Org. Logo Width/Height : (308/111)
4.  Organization Title     : SquirrelMail $version
5.  Signout Page           : 
6.  Top Frame              : _top
7.  Provider link          : http://squirrelmail.org/
8.  Provider name          : Unixmen Mail

R   Return to Main Menu
C   Turn color off
S   Save data
Q   Quit

Command >> S

Now, enter “2” to setup mail Server settings such as domain name and mail agent etc.:

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

D.  Set pre-defined settings for specific IMAP servers

C   Turn color off
S   Save data
Q   Quit

Command >> 2

Enter “1”, Enter your mail domain (ex. unixmen. local) and press Enter key.

SquirrelMail Configuration : Read: config.php (1.4.0)
---------------------------------------------------------
Server Settings

General
-------
1. Domain : localhost
2. Invert Time : false
3. Sendmail or SMTP : Sendmail

A. Update IMAP Settings : localhost:143 (uw)
B. Change Sendmail Config : /usr/sbin/sendmail

R Return to Main Menu
C Turn color off
S Save data
Q Quit

Command >> 1

The domain name is the suffix at the end of all email addresses. If
for example, your email address is [email protected], then your domain
would be example.com.

[localhost]: unixmen.local

Enter “3” and change from sendmail to Postfix MTA (i.e. SMTP):

SquirrelMail Configuration : Read: config.php (1.4.0)
---------------------------------------------------------
Server Settings

General
-------
1.  Domain                 : unixmen.local
2.  Invert Time            : false
3.  Sendmail or SMTP       : Sendmail

A.  Update IMAP Settings   : localhost:143 (uw)
B.  Change Sendmail Config : /usr/sbin/sendmail

R   Return to Main Menu
C   Turn color off
S   Save data
Q   Quit

Command >> 3

Enter “2” to switch from sendmail MTA to postfix.

You now need to choose the method that you will use for sending
messages in SquirrelMail.  You can either connect to an SMTP server
or use sendmail directly.

  1.  Sendmail
  2.  SMTP
Your choice [1/2] [1]: 2

Now enter “S” followed by “Q” to save and exit Squirrelmail configuration.

Create a squirrelmail vhost in apache config file:

vi /etc/httpd/conf/httpd.conf

Add the following lines at the end:

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>

Restart the Apache service:

systemctl restart httpd

Create mail users

Create some users for testing. In my case I create two users namely senthil and kumar.

useradd senthil
passwd senthil
useradd kumar
passwd kumar

Access Webmail

Now navigate to http://ip-address/webmail or http://domain-name/webmail from your browser.

The following screen should appear. Enter the username and password of the user which we have created earlier.

Unixmen - Login - Chromium_003

Now, you’ll be able to access the user mail box.

SquirrelMail 1.4.22-15.el7 - Chromium_004

Compose mails

Let us compose a test mail from user senthil to userkumar. Click on the Compose link on the top. Enter the recipient mail id (ex. [email protected].local), subject and body of the mail and click Send.

SquirrelMail 1.4.22-15.el7 - Chromium_005

Now, log out from user senthil and log in to user kumar mail and check for any new mail.

SquirrelMail 1.4.22-15.el7 - Chromium_006

Hurrah! We have got a new mail from [email protected].local mail id.

To read the mail, click on it. You’ll now be able to read, reply, delete or compose a new mail.

SquirrelMail 1.4.22-15.el7 - Chromium_007

That’s all for now. We’ve successfully configured a local mail server that will serve in/out mails within a local area network.

Hope this tutorial will help you.

Good luck!

To setup mail server in CentOS 6.x server, refer the following link.

Reference links:

  • stuyizere

    This is a selinux configuration.

    You need to allow httpd to send emails by running the following command:

    $ sudo setsebool httpd_can_sendmail 1

  • stuyizere

    To make this persistent across reboots, you need to add -P

    $ sudo setsebool -P httpd_can_sendmail 1

  • Hakan Kara

    It works well until reboot the sytem. After reboot the system does not start and grub cli (grub>) waiting for me to write something. What could be the reason ? , I did same thing two times and the result is always same.

  • Muhammad Tahir Minhas

    I have done all the step but enable to login through web mail (Squirrel Mail). Please help me.