Q: I am trying to configure postfix to send email through my ISP SMTP service or my shared hosting services external SMTP.  I found a lot of tutorials via google, but none seems to work for me.

A: First, you should check with your ISP and make sure they allow SMTP connections, some ISPs block port 25.  Here are the ininstructions you will need to be able to send mail from postfix through your ISP SMTP server.

Ensure postfix is installed.

CentOS / Red Hat

yum install postfix -y

Fedora (28 and above)

dnf install postfix -y

Ubuntu / Debian

sudo DEBIAN_FRONTEND=noninteractive apt-get install postfix -y

Second, remove sendmail if you have it installed.  Red Hat 5, CentOS 5 and Fedora versions older than 15 all use sendmail by default.  Red Hat 6, CentOS 6, and Fedora 15 and above us postfix by default (I believe).

yum remove sendmail -y

NOTE: Make sure you are root, or using sudo. You need root permissions to edit the file.
Now open /etc/postfix/main.cf in your favorite editor. I used vi (vim) for this, but you can use nano, or even gedit.

NOTE: A lot of SMTP servers use ports OTHER than port 25.  In my case it use 465, make sure you change the first line to match your SMTP providers information.
Add the following lines to the end of the /etc/postfix/main.cf file:

relayhost = smtp.domain.com:465
smtpd_sasl_auth_enable = yes
smtpd_sasl_path = smtpd
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_type = cyrus
smtp_sasl_auth_enable = yes

Create a file called /etc/postfix/sasl_passwd.  The file should have just one line, your SMTP relay server, port, username and password. It is very important to make sure the first part (server:port) matches what you entered into relayhost in /etc/postfix/main.cf exactly.  

Here is an example:

smtp.domain.com:465 user:password

Now you need to generate a hashed version postfix can use.  We will use postmap command like so:

postmap hash:/etc/postfix/sasl_passwd

Let’s test postfix to make sure it hashed the file and all is well (Remember to use your SMTP information here).

postmap -q smtp.domain.com:465 user:password

The above command should generate an output that shows your user and password that you entered into the sasl_passwd file.  If everything looks good you can now delete the /etc/psotfix/sasl_passwd file for security reasons.

rm /etc/postfix/sasl_passwd

Now restart postfix, and set it to start on boot.

On Red Hat 7, CentOS 7, Ubuntu or Fedora versions 15 and above:

systemctl restart postfix.service
systemctl enable postfix.service

On Red Hat 6, CentOS 6, or Fedora versions below 15:

service postfix restart
chkconfig postfix on

That’s it, your system should now be able to send mail through the configured SMTP server.