How To Install Postfix CentOS 7

Published on 2019-08-21Β· Updated on 2024-01-25

The author voluntarily contributed this tutorial as a part of Pepipost Write to Contribute program.

Introduction

Postfix is a popular open-source Mail Transfer Agent (MTA) for routing and delivering mail. It is an alternative to the pre-installed Sendmail MTA in versions prior to CentOS/RHEL 5. 

The primary function of CentOS's Postfix is to relay emails locally or to an external server. To install Postfix without conflicts, remove Sendmail if it's installed.

Step 1: Checking And Removing Sendmail (Required Only If Sendmail Is Installed)

Input: Run the below command to check whether sendmail is installed or not:

rpm -qa | grep sendmail

Output: If Sendmail has been installed on your server, the following output will appear:

If you did not receive any output, it is most likely that Sendmail is not installed and you can proceed to step 2. 

If you receive an output similar to the one shown above, remove Sendmail by executing the command below:

sudo yum remove sendmail*

Once Sendmail has been successfully removed, you will receive a similar output as shown below:

Step 2: Install Postfix

Installing postfix using the yum installer is one of the simplest methods. (If you are using Centos/RHEL versions 5 or above, Postfix should be installed in the system.).

You can check if Postfix is installed on CentOS 7 by using the command below:

rpm -qa | grep postfix

If it’s installed, you will receive the output shown above.

In case Postfix is not installed, use the below command to install it:

sudo yum install postfix

Keep saying β€˜Yes’ to each prompt until all components are downloaded. This will result in a successful installation of Postfix CentOS 7.

Step 3: Configure Postfix.

We need to edit /etc/postfix/main.cf file.

vim /etc/postfix/main.cf

Make changes according to the below steps.

Note: You will find the line which needs to be changed on line 67.

Add hostname to the file by unhashing and editing at line number 75.

myhostname = smtp.example.local

Uncomment and set domain name at line no 83

mydomain = example.local

Uncomment line no 99

myorigin = $mydomain

Uncomment and Set ipv4 at line no 113

inet_interfaces = all

edit line no 119 to all

inet_protocols = all

Comment at line no 164

mydestination = $myhostname, localhost.$mydomain, localhost,

Uncomment and add IP range at line no 264

mynetworks = 192.168.1.0/24, 127.0.0.0/8

Uncomment at line no 419

home_mailbox = Maildir/

save and exit the file.

Enable the service using the below command

systemctl enable postfix

Start/restart the postfix service.

systemctl restart postfix

once you have restarted postfix need check the status of the service using below command:

systemctl status postfix 
OR 
service postfix status
[root@nl587 ~]# service postfix status
Redirecting to /bin/systemctl status postfix.service
● postfix.service - Postfix Mail Transport Agent
Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2019-08-04 18:02:45 IST; 51min ago
Main PID: 4501 (master)
CGroup: /system.slice/postfix.service
β”œβ”€ 4501 /usr/libexec/postfix/master -w
β”œβ”€ 4506 pickup -l -t unix -u
β”œβ”€ 4507 qmgr -l -t unix -u
β”œβ”€15652 smtpd -n smtp -t inet -u -s 2
β”œβ”€15653 proxymap -t unix -u
└─15654 anvil -l -t unix -u

Aug 04 18:02:08 nl587 systemd[1]: Starting Postfix Mail Transport Agent...

Step 4: Testing Postfix Server

Create a user for testing and name it "postfixtester"

useradd postfixtester

add password for the user postfixtester

passwd postfixtester

After adding a user, you need to check the server access. Let’s use telnet for that.

telnet localhost smtp

Once you have successfully done with the configuration you get the following output
[root@nl587 ~]# telnet localhost smtp
Trying ::1…
Connected to localhost.
Escape character is '^]'.
220 smtp.example.local ESMTP Postfix

Start your transaction writing below command.

ehlo localhost
250-smtp.example.local
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN

Once you get 250 DSN you can sendmail.

Case 1: Successful Test case

mail from:<postfixtester>
250 2.1.0 Ok [success output]
# rcpt to:<postfixtester>
250 2.1.5 Ok [success output]
# data # type this to pass input of email body
354 End data with <CR><LF>.<CR><LF> [success output ]
This is test mail from your local host server #mail body
. # dot(.) for completing the mail body
250 2.0.0 Ok: queued as E2R572042D75 [success output]
quit # quit from the telnet command prompt
221 2.0.0 Bye
Connection closed by foreign host.

Case 2: Failed Testcase case

mail from:<postfixtester>
250 2.1.0 Ok [success output]
# rcpt to:<postfixtester>
454 4.7.1 <postfixtester>: Relay access denied [failure point]

This error occurs when your domain is not mapped with the server. For instance, if you mention 'mydomain = example.com' in the '/etc/postfix/main.cf' file and it is not mapped with your hostname/server, Postfix will show the above error.

ls /home/postfixtester/Maildir/new/

Output:

1336588056.Vfe01I786e3e7M938078.smtp.example.local

If you received the above output, you would have successfully received an email. To read the email, simply use the β€˜cat’ command on the file.

cat /home/postfixtester/Maildir/new/1336588056.Vfe01I786e3e7M938078.smtp.example.local

Output:

Return-Path: <[email protected]>
X-Original-To: postfixtester
Delivered-To: [email protected]
Received: from localhost (localhost [IPv6:::1])
by smtp.example.local (Postfix) with ESMTP id E2R572042D75
for <postfixtester>; Sun, o4 Aug 2019 17:42:36 +0530 (IST)
Message-Id: <[email protected]>
Date: Sun, 04 Aug 2019 17:42:36 +0530 (IST)
From: [email protected]

TThis is the test mail from your localhost server.

Great news! Your Postfix installation is complete, and you can now use your server as a private SMTP server to send emails. 

These were some simple steps to install, configure, and text Postfix. 

If you have further queries, feel free to drop an email to [email protected], and we would be happy to help you.
<!-- Happy Coding />

Excited about the latest in Bulk Email Marketing! Check out this insightful blog on Gmail and Yahoo updates in the email marketing landscape.
Explore the Blog - Here
Stay ahead of the game with valuable insights on optimizing your email campaigns! πŸ“¬

Grade My Email
Check your spam now?

Netcorecloud's toolkit is the solution to all your email problems.

Vikram Sahu πŸ¦‰

Developer Evangelist, Pepipost

πŸ’» Senior Software engineer β†’ πŸ₯‘ Developer Relations guy β†’ πŸ€“ Building Community for Email Geeks 🎟 β†’ Speaker πŸŽ™

You can also explore

Netcore connects & unifies your data across all sources, connects to your marketing channels and provides you with control over AI Powered automation and personalization.

Deploy emails that are
screenshot worthy!

Stop settling for static emails - use Netcore's platform to create dynamic, interactive emails that convert.
Let's Deploy
Get Started Now