This tutorial shows you how to configure Postfix mail server on Ubuntu 22.04 to relay email through Gmail. The first step is to install Postfix (if you have not done that yet).
Install Postfix SMTP server
sudo apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules
Configure Postfix to relay email
Now open the Postfix main configuration file /etc/postfix/main.cf in a text editor. I will use the nano editor here:
sudo nano /etc/postfix/main.cf
Add the following lines to the end of the file:
# Relay settings 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_use_tls = yes
Next, we will have to create a file containing the Gmail username and password. This file will be named sasl_passwd file. There we add our Gmail account credentials:
sudo nano /etc/postfix/sasl_passwd
Replace the username and password with your Gmail user and password. Add the following line:
Save and close the file.
Protect the password in the sasl_passwd file by making it only readable by root:
sudo chmod 600 /etc/postfix/sasl_passwd
Now we have to use postmap command to create a hashed version of the file:
sudo postmap /etc/postfix/sasl_passwd
Finally, restart Postfix to apply the changes:
sudo systemctl restart postfix
Now Postfix will relay all outgoing emails through your Gmail account. Please note that Gmail may require you to allow less secure apps to access your account for this to work.