How to setup Simple Mail Transfer Protocol (SMTP) in RHEL 7

How to setup Simple Mail Transfer Protocol (SMTP)

In general, the sender will create the email message and sends to server. Then it will be forwarded to recipient mail server and delivers the mail to client progtram or web mail. The SMTP server service is present in all the servers. In this article we will discuss how to setup Simple Mail Transfer Protocol (SMTP) in RHEL 7.

Test environment

Original mail server and central mail server

Original Mail Server: (hostname: mail.linuxhelp.com / IP: 192.168.5.159)

/etc/hosts file is used for the name resolution.

192.168.5.159 mail.linuxhelp.com mail
::1 mail.linuxhelp.com mail

To install Postfix and Firewall

To Install Postfix

[root@linuxhelp ~]# yum install postfix
Loaded plugins: fastestmirror, langpacks
base | 3.6 kB 00:00:00
extras 3.4 kB 00:00:00
updates 3.4 kB 00:00:00
updates/7/x86_64/primary_db 3.2 MB 00:00:04

Loading mirror speeds from cached hostfile

* base: centos-hn.viettelidc.com.vn

Most of linux distribution postfix is default package

To Start the service

You can enable it to run on the future reboots

[root@linuxhelp ~]# systemctl start postfix
[root@linuxhelp ~]# systemctl enable postfix

To allow mail traffic through the firewall

[root@linuxhelp ~]# firewall-cmd --permanent &ndash add-service=smtp

success

[root@linuxhelp ~]# firewall-cmd &ndash add-service=smtp

success

To Configure Postfix
You can configure postfix on mail.linuxhelp.com. The /etc/postfix/main.cf consists of the postfix main configuration file.

To Configure Postfix on mail.linuxhelp.com.

[root@linuxhelp ~]# vim /etc/postfix/main.cf

myhostname = mail.linuxhelp.com
mydomain = linuxhelp.com
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
mynetworks = 192.168.5.0/24, 127.0.0.0/8

To Restart the service

[root@linuxhelp ~]# systemctl restart postfix
the servers can be restarted

To troubleshoot

[root@linuxhelp ~]#systemctl status postfix
postfix.service - Postfix Mail Transport Agent
Loaded: loaded (/usr/lib/systemd/system/postfix.service  enabled)
Active: active (running) since Thu 2016-03-17 23:56:36 EDT  1min 40s ago
Process: 6424 ExecStop=/usr/sbin/postfix stop (code=exited, status=0/SUCCESS)
Process: 6439 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)

[root@linuxhelp ~]# journalctl -xn

-- Logs begin at Thu 2016-03-17 22:21:47 EDT, end at Fri 2016-03-18 00:01:08 EDT
Mar 18 00:01:01 linuxhelp systemd[1]: Starting Session 13 of user root.
-- Subject: Unit session-13.scope has begun with start-up
...
...
...
-- The start-up result is done.
Mar 18 00:01:01 linuxhelp CROND[6643]: (root) CMD (run-parts /etc/cron.hourly)
Mar 18 00:01:01 linuxhelp run-parts(/etc/cron.hourly)[6649]: start

[root@linuxhelp ~]# postconf -n
alias_database = hash:/etc/aliases
alias_maps = hash:/etc/aliases
command_directory = /usr/sbin
config_directory = /etc/postfix
daemon_directory = /usr/libexec/postfix
data_directory = /var/lib/postfix
debug_peer_level = 2
debugger_command = PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin ddd $daemon_directory/$process_name $process_id &  sleep 5
html_directory = no
inet_interfaces = localhost
inet_protocols = all
mail_owner = postfix
.....
.....

To test Postfix Mail Servers

Mail User Agent can be used to test the mail servers.

[root@linuxhelp ~]# mail -S " hai"  user1@linuxhelp.com
Subject: invite
wellcome to linuxhelp tutoraial...
.
EOT

To check if email is received

Go to central mail server and log on as user

[root@linuxhelp ~]# su - user1
[user1@linuxhelp ~]$ mail

Heirloom Mail version 12.5 7/5/10. Type ? for help.
" /var/spool/mail/user1" : 2 messages 1 new 2 unread
U 1 root Thu Mar 17 22:51 21/590 " hai" 
> N 2 root Fri Mar 18 00:14 18/606 " invite" 
&  ^Cinterrupt
You have mail in /var/spool/mail/user1

To check the mail content

[user1@linuxhelp ~]$ vim /var/mail/user1

From root@linuxhelp.com Fri Mar 18 00:14:21 2016
Return-Path: < root@linuxhelp.com> 
X-Original-To: user1@linuxhelp.com
Delivered-To: user1@linuxhelp.com

...
...

Message-Id: < 20160318041421.397E46316722@mail.linuxhelp.com> 
From: root@linuxhelp.com (root)
Status: O

welcome to linuxhelp tutorial...

If mail is not received, check root mail spool for error notification.

Tag : SMTP
FAQ
Q
What are all the models of SMTP Protocols?
A
The SMTP model is of two type :

End-to- end method
Store-and- forward method

The end to end model is used to communicate between different organizations whereas the store and forward method is used within an organization
Q
Where i can check the mail content of the received mails ?
A
You can find your mail contents Under "/var/mail/username"
Q
I don’t have any purchased domain for accessing the domain i have created is there any way to check with the configuration?
A
Put a host entry under "/etc/hosts" so that you can view at the Local amchine
Q
I can't able to send mails eventhough my postfix status is active?
A
Please verify the entries in firewall whether you have configured and allowed the ports properly.
Q
How can i Troubleshoot postfix?
A
You can us ethe "journalctl -xn" and "postconf -n"
to troubleshoot the Postfix.