Configure Postfix

This these guides configure your postfix so its forwards all local mails to an external server using authentication.

Relay all Mail

This is the content of the entire /etc/postfix/main.cf the servers name is ns1.fnf.nu the mail going out comes as root@dalesjo.com instead of root@ns1.fnf.nu. all mail is relayed to 192.168.61.114.

inet_interfaces = loopback-only
mydestination=
myhostname = ns1.fnf.nu
mynetworks=127.0.0.0/8 [::1]/128
myorigin = dalesjo.com
relayhost = 192.168.61.114
local_transport=error: local delivery disabled

https://www.rootusers.com/configure-postfix-to-forward-mail-to-a-central-relay-server/

Authenticate against relay

Install sasl to enable authentication.

yum -y install postfix cyrus-sasl-plain mailx

Add the code below to /etc/postfix/main.cf

smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous

/etc/postfix/sasl_passwd is where username and password is stored

echo "[192.168.61.114]:587 myusername:secret" > nano /etc/postfix/sasl_passwd
postmap /etc/postfix/sasl_passwd
chown root:postfix /etc/postfix/sasl_passwd*
chmod 640 /etc/postfix/sasl_passwd
systemctl restart postfix

https://devops.profitbricks.com/tutorials/configure-a-postfix-relay-through-gmail-on-centos-7/

Testing

echo "Testing" | mail -s "Test Message 4" my@email.com

Errors

If you get errors like the one below, it might be that you missed to install the package cyrus-sasl-plain

(delivery temporarily suspended: SASL authentication failed; cannot authenticate to server example.com[1.1.1.1]: no mechanism available)