How to Install and Configure Postfix on Centos 7

Installing Postfix

Postfix comes pre-installed on the CentOS 7 by default, but here's the command to install it as well.

  yum install postfix

Run the below command to check whether postfix is installed or not.

  rpm -qa | grep postfix

Output:

 [root@vps]# rpm -qa | grep postfix
 postfix-2.10.1-9.el7.x86_64

After Postfix is installed, you can start the service and enable it to make sure it starts after reboot.

  systemctl start postfix

  systemctl enable postfix

Configuring Postfix

All of the options you need for the service are located in /etc/postfix. The main configuration file for the Postfix service is located at /etc/postfix/main.cf. Within the configuration file, there are many options that you can add, some of them more common than others.

 nano /etc/postfix/main.cf

Make changes according to the below steps.

Myhostname declares the mail server’s hostname. Hostnames normally have prefixes in them, like this:

  myhostname = mail.example.com

Mydomain declares the domain that is actually handling mail, like this.

  mydomain = example.com

All emails sent from this mail server will look as though it came from this option. You need to set this parameter to the main hostname of the server.

  myorigin = $mydomain

The mydestination parameter defines the domains from which the server will accept mail. It can take values like this.

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

mail_spool_directory directory where mailbox files are kept.

  mail_spool_directory = /var/spool/mail

Mynetworks declares a list of trusted remote SMTP servers that can relay through the server, like this.

  mynetworks = 168.100.189.0/28, 127.0.0.0/8

This option specifies the IP protocol version used for server connections.

  inet_protocols = ipv4

save and exit the file.

Start/restart the postfix service.

  systemctl reload postfix

You can check for errors using the following command.

  postfix check

Testing postfix Server

To install mailx using the following command.

  yum install -y mailx

You can send test mail using following command.

  echo "This is message body" | mailx -s "This is Subject" -r "test@example.com" someone@example.com

You can send test mail With including attachments using following command.

  $ echo "This is message body" | mailx -s "This is Subject" -r "test@example.com" -a /path/to/attachment someone@example.com

Sender : test@example.com Receiver: someone@example.com

Finally! You have postfix installed and emails getting sent!