Postfix Gmail SMTP Relay
December 16th, 2019 / By Andrew Currie
December 16th, 2019 / By Andrew Currie
Postfix is a robust mail server freely available on almost all Linux systems. In this post I’ll explain how you can easily configure your Postfix mail server to relay your outbound mail to the Gmail SMTP server. To complete this exercise you will need the following:
Let’s start by making sure you have the Postfix, SASL Authentication and mailx packages installed. To do this issue the command below:
yum -y install postfix cyrus-sasl-plain mailx
Next your going to restart your Postfix mail server so that it will detect your SASL Authentication Framework:
systemctl restart postfix
You should also make sure that Postfix is configure to run on boot by issuing the following command:
systemctl enable postfix
Now we are ready to proceed and configure your Postfix server to relay your outbound emails to the Gmail SMTP server using your Gmail account credentials. To do this we need to open the Postfix configuration file and add some settings to the bottom of the file. I prefer nano but you can also use vim or vi if you are so inclined. Issue the following command to open up the Postfix configuration file:
sudo nano /etc/postfix/main.cf
Inside the configuration file press Control + V repeatedly until you have scrolled all the way to the bottom of the page then enter the following code:
relayhost = [smtp.gmail.com]:587
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
Press Control + O then Enter to write the changes to disk. Then press Control + X to exit nano.
The next step is to provide your Gmail account credentials in your SASL authentication file. To do this we issue the following command to create a new SASL file:
sudo nano /etc/postfix/sasl_passwd
Inside the nano editor enter your Gmail credentials in the following format:
[smtp.gmail.com]:587 YOUR-GMAIL-EMAIL-ADDRESS-HERE:YOUR-GMAIL-PASSWORD-HERE
Press Control + O then Enter to write the changes to disk. Then press Control + X to exit nano.
Now that’s done we have to let Postfix know where your Gmail credentials are located. Issue the following command to do just that:
postmap /etc/postfix/sasl_passwd
For security purposes you need to tighten the permissions on your sasl_passwd file, to do this issue the following commands:
chown root:postfix /etc/postfix/sasl_passwd
chmod 640 /etc/postfix/sasl_passwd
Almost done, now you just need to tell Postfix to reload it’s configuration then you can send a test email. To do this enter the following command:
systemctl reload postfix
systemctl restart postfix
Now send yourself a test email to make sure your new Postfix Gmail SMTP relay is working. You can use the following example command to do this:
echo "This is a Postfix Gmail SMTP relay test. | mail -s "Test Email" yourname@yourdomain.com
I hope you found this post useful. If you have any comments, feedback or suggestions, feel free to contact us, Thanks!