Table of Contents
Few days ago I wrote a post about how to install and configure sSMTP, a simple and free alternative to Postfix and Sendmail which can be used to send e-mail through external SMTP services hosted by providers such as GMail, Aruba, Yahoo and so on.
Although sSMTP is a great piece of software, those who prefer to setup the real deal will definitely benefit from this article, in which I’ll explain how to setup and configure Postfix 3 – the latest installment of the most efficient, secure and widely used mail server born as a (better) alternative to Sendmail.
Before proceeding, it’s important to emphasize the fact that this article will only explain how to send e-mail using an external SMTP service using Postfix, being it nothing more than 10% of what this awesome piece of software can actually do. If you need further info about Postfix and its features, I strongly suggest to take a look on the Postfix official documentation at postfix.org. Also, the instructions below will be ok for a CentOS / Fedora / RHEL Linux machine: although the tutorial will also work for any other distribution, Ubuntu and Debian users might have to slightly adapt some commands to their package managers.
Step 1: Check Postfix version
The first thing we should do is to check if Postfix is already installed within our system, and – in case it is – the installed version. This can easily be done by executing the following terminal command:
# postconf -d | grep mail_version
Regardless of how recent our CentOS version is, there’s an high chance that we’ll still have Postfix 2. Although such version can be good enough for most users, it has very limited capabilities when dealing with the deprecated SMTPS service, also known as SMTP over SSL (on TCP port 465). In short words, this basically means that – whenever we try to connect to a SMTP server using such protocol – it will most likely fail with the following error message in the /var/log/maillog file:
CLIENT wrappermode (port smtps/465) is unimplemented
instead, send to (port submission/587) with STARTTLS
Unfortunately such issue is not easy to overcome with Postfix 2, as the whole 2.x version tree does not natively support SMTP on SSL through TCP port 465. Our only chance to make the connection work is to either use the TCP port 587 and use STARTTLS – which is natively supported – or to forward the connection through Stunnel or other tools that can perform SSL tunneling of some sort.
Luckily enough, setting up a SSL tunnel is not the only option: we can also upgrade to Postfix 3, which features a built-in SMTPS support thanks to its new TLS Wrapper Mode feature.
Step 2: Uninstall Postfix 2 / Sendmail / sSMTP
The next thing we have to do is to ensure that there aren’t any other mail sending softwares configured within our server, unless we really want to keep Postfix 3 and one (or more) of them. In case we don’t, we can easily uninstall all of them with the following terminal commands:
# sudo yum remove postfix
# sudo yum remove ssmtp
# sudo yum remove sendmail
Although performing a yum remove is a required step for Postfix – assuming we want to install a newer version – we could also choose to systemctl disable and systemctl stop sSMTP and/or Sendmail instead of removing them. The only important thing to do here is to ensure that neither of them will be up and running, otherwise they will prevent our soon-to-be-installed Postfix 3 from working properly.
Step 3: Install Postfix 3
Now it’s the time to install Postfix 3. It’s package name is postfix3, however there’s a high chance that – if we just attempt a sudo yum install postfix3 – we’ll get something like this:
No package postfix3 available.
Unfortunately, the postfix3 package isn’t (yet) included in the default CentOS 7 repositories. In order to install it, we have to add a third-party repository – such as Ghettoforge‘s – to our repo list. Again, in case we’re not using CentOS, we can choose a different repo from the official Postfix Packages and Ports page.
To add the Ghettoforge repo to your YUM repository list, create a new /etc/yum.repos.d/gf.repo file using nano, vim or any other text editor and fill it with the following contents:
name=Ghettoforge packages that won't overwrite core distro packages.
name=Ghettoforge packages that will overwrite core distro packages.
# Please read http://ghettoforge.org/index.php/Usage *before* enabling this repository!
Before using that repo we’ll also have to download the RPM-GPG-KEY-gf.el7 file from the Ghettoforge Key Page and save it within our /etc/pki/rpm-gpg/ folder.
Once done, we’ll be able to issue the sudo yum install postfix3 terminal command and have Postfix 3.2.x (at the time of writing) installed in few seconds. Don’t forget to also execute a sudo yum enable postfix to ensure that the service will start upon each boot.
Step 4: Postfix 3 Setup
Now that Postfix 3 is installed on our system, we just have to configure it to have our e-mail messages sent through the external SMTP service. The configuration involves three files:
- The /etc/postfix/master.cf file, where we need to enable the smtps support.
- The /etc/postfix/sasl_passwd file, which we have to create and fill it with the login credentials to connect to our external SMTP server(s).
- The /etc/postfix/main.cf file, where we’ll configure the service and tell it the SMTP service/account to use.
4.1: Enable SMTPS support
Open the /etc/postfix/master.cf file and uncomment (or add) the following lines to enable the smtps support:
smtps inet n - n - - smtpd
4.2: Setup Connection Info
Open the /etc/postfix/sasl_passwd file, or create it if it doesn’t exist yet, and add your external SMTP servers and their respective login credentials, one per line, using the following format:
The square brackets are not mandatory, they will just make the system skip the DNS probe to these servers.
Once done, open a terminal window and type the following command to encrypt the password file:
# sudo postmap /etc/postfix/sasl_passwd
The command will encrypt the plain-text connection info settings into a new encrypted /etc/postfix/sasl_passwd.db file. Before going further, it could be wise – for security reasons – to restrict both files permissions in the following way:
# sudo chown root:root /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
# sudo chmod 0600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
4.3: Configure Postfix
It’s now time to open the /etc/postfix/main.cf file and finalize the Postfix setup by adding / uncommenting the following parameters and configure them in the following way:
# receive mail only from localhost (prevents spammers from using your SMTP service)
inet_interfaces = localhost
# Enable IPv4, and IPv6 if supported
inet_protocols = all
# Set the external SMTP service to connect to (login info must be present in the sasl_passwd.db file)
relayhost = [smtps.aruba.it]:465
# enable SASL authentication
smtp_sasl_auth_enable = yes
# disallow methods that allow anonymous authentication.
smtp_sasl_security_options = noanonymous
# where to find sasl_passwd (the sasl_passwd.db file location)
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
# Enable STARTTLS encryption
# smtp_use_tls = yes
# TLS Security Level (encrypt = accept all certificates)
smtp_tls_security_level = encrypt
#enable Wrapper mode
smtp_tls_wrappermode = yes
In case you have a public hostname you want to be shown in the Return-Path mail header, it wouldn’t hurt to also set the myhostname and mydomain parameters in the following way:
myhostname = www.yourserverhostname.com
mydomain = yourserverhostname.com
Step 5: Test run
We’re ready for a test run to check that everything is working properly.
Before doing that, be sure to restart the Postfix service and reload the configuration by issuing the following commands:
# systemctl restart postfix
# postfix reload
Once done, we can send a test e-mail to a real e-mail address under our own control in the following way:
# sendmail firstname.lastname@example.org
Subject: Postfix 3 Test
If you read this, it means that everything is working properly!
Right after that, we can check our mailbox and ensure that the e-mail are being sent.
In case you don’t receive the e-mail, the best thing you can do is to check the postfix log file at the following path:
And see what is going wrong. For example, if you find something like this:
status=bounced (unknown user: "target")
It probably means that you configured your postfix to use a myhostname and/or mydomain value that matches the domain part of target‘s e-mail, thus making it look for a local target user instead of relying the e-mail remotely. To fix that, replace these values or add a relay fallback that will tell postfix what to do if there are no local users matching that name.:
fallback_transport = relay
Right after that, restart and reload postfix.
That’s about it: I sincerely hope that this tutorial will help other System Administrator who wish to install Postfix on their system!