Ubuntu: configure postfix to use Gmail as SMTP relay

Install postfix and some tools

sudo apt-get install postfix mailutils libsasl2–2 ca-certificates libsasl2-modules

Create the configuration files

sudo touch /etc/postfix/main.cf
sudo touch /etc/postfix/sasl_passwd

Paste the following in /etc/postfix/main.cf

relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CApath = /etc/ssl/certs
smtpd_tls_CApath = /etc/ssl/certs
smtp_use_tls = yes

Paste the following in /etc/postfix/sasl_passwd, replace ‘USERNAME’, ‘PASSWORD’.

[smtp.gmail.com]:587 USERNAME@gmail.com:PASSWORD

Correct file permissions

sudo chmod 400 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd

reload postfix for changes to take effect

sudo systemctl restart postfix

Test postfix configuration, replace username@gmail.com

echo "This is a test :)" | mail -s "host: $(hostname -s) — [THIS IS A TEST]" username@gmail.com

DevOps engineer, loves Linux, Python, cats and Amiga computers

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store