How to install and configure Postfix smtp relay using Gmail on ubuntu 16.04 | 18.04 | 20.04

Postfix is an Mail Transfer Agent , Its is very powerful and easy to configure email server, On this post we will install and configure smtp relay using Gmail.

To configure Postfix as a relay smtp server using Gmail, You must have a Gmail email id and password as well as You have allow less-secure in your gmail account to allow postfix to use the gmail credentials.

Updating hostname

Step 1:- To install all required dependencies by following commands with root privileges, You must set your machine hostname like mail.example.com

sudo -i


hostnamectl set-hostname mail.example.com

Now you need to reboot the ubuntu machine to get effects on machine hostname.

Installation dependencies and Postfix packages.

Step 2:- After this we can performed the required packages.

apt-get install postfix  libsasl2-2 mailutils ca-certificates libsasl2-modules -y

Note :- While installing Postfix, You wiil be asked for hostname name you need enter your mail.example.com after configuration of your FQDN within postfix, We are ready to configure smmtp relay with Gmail credentials.

Step 3:- After this We need to specified our Gmail username and password by following the commands.

vim /etc/postfix/sasl_passwd

You need to replace your username and password in the file

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

Save and exit from the editor.

Postfix configuration

Step 4:- In next step, You need to open the postfix mail config with by using following commands.

vim /etc/postfix/main.cf

Add the following the lines.

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 and exit from your editor.

Update the permission

Step 5:- Now you need to update the permission of sasl_passwd file:

chmod 400 /etc/postfix/sasl_passwd

postmap /etc/postfix/sasl_passwd

Step 6:- After this you need to verify the certificate certificates by following the commands.

cat /etc/ssl/certs/thawte_Primary_Root_CA.pem | sudo tee -a /etc/postfix/cacert.pem

You should get output like this :-

-----BEGIN CERTIFICATE-----


MIIEIDCCAwigAwIBAgIQNE7VVyDV7exJ9C/ON9srbTANBgkqhkiG9w0BAQUFADCB
qTELMAkGA1UEBhMCVVMxFTATBgNVBAoTDHRoYXd0ZSwgSW5jLjEoMCYGA1UECxMf
Q2VydGlmaWNh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4XI5ISXycO1cRrK1zN44veFyQaEfZYGDm/Ac9IiAX
xPcW6cTYcvnIc3zfFi8VqT79aie2oetaupgf1eNNZAqdE8hhuvU5HIe6uL17In/2
/qxAeeWsEG89jxt5dovEN7MhGITlNgDrYyCZuen+MwS7QcjBAvlEYyCegc5C09Y/
LHbTY5xZ3Y+m4Q6gLkH3LpVHz7z9M/P2C2F+fpErgUfCJzDupxBdN49cOSvkBPB7
jVaMaA==
-----END CERTIFICATE-----

 

In  case you did not get ant signature of the certificate so then you need to use

to Thawte_Premium_Server_CA.pem 

Restart the Postfix service

Step 7:- After getting certificate signature details all thing is going good, Now the time has come to restart the postfix the services to get effects.

systemctl restart postfix

Testing Postfix 

Step 8:- After this we need to test the postfix is working or not, To do that you need to You need to use the given commands to sent a test email to your email address to verify its proper working or not.

echo "Postfix email from smtp relay server" | mail -s "Testing Postfix" username@example.com

Note :- If you want to use postfix smtp relay in your monitoring tools like Nagios you have postfix commands, To get your mail command path by using the following cli.

which mail

You should get output like this – /usr/bin/mail this is use in your Nagios SMTP configuration with Postfix.

How to install and configure Postfix smtp relay using Gmail on ubuntu 16.04 | 18.04 | 20.04

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top