1.configure Postfix
Edit the Postfix configuration file.
sudo nano /etc/postfix/main.cf
Find the following line relayhost = about 6 lines up from the bottom of the file and delete it.
Add the following to the end of the file.
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_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes
Save file and exit. (Press CTRL + X, press Y and then press ENTER)
2. Create Password and DB Files
Create the sasl_passwd file which will store our credentials.
sudo nano /etc/postfix/sasl_passwd
Insert the following:
[smtp.gmail.com]:587 username@gmail.com:password
Save file and exit. (Press CTRL + X, press Y and then press ENTER)
Create a hash database file for Postfix with the postmap command.
sudo postmap /etc/postfix/sasl_passwd
There should now be a file called sasl_passwd.db in the /etc/postfix/ directory.
For added security, we will only allow root user to read and write to sasl_passwd and sasl_passwd.db
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
3.Send a Test Mail
We’ll now send a test email message. Make sure to replace test@example.com with your own email address.
echo "Test Email message body" | mail -s "Email test subject" test@example.com