Configure Postfix to use Gmail SMTP in Ubuntu

CSCC LABS
5 min readNov 7, 2020

--

1. What is Postfix?

Postfix is a free and open-source mail transfer agent(MTA) that routes and delivers electronic mail. It is released under the IBM Public License 1.0 which is a free software license.

2. Changing the Hostname on Ubuntu Server

Before we configure Postfix MTA, let’s adjust our hostname to reflect the correct domain name in our outgoing internal email.

Setting up a hostname is really important if you’re using a custom domain for emailing. The hostname helps in proper mailing address.

About Hostname, you can check your current hostname using the below command line in SSH.

hostname -f

To change the hostname permanently we have to change at three places

  1. In Command line

I will be changing the hostname to kashy.com using the below command.

hostname kashy.com

2. However, in order to make this change permanent we have to edit, hostname and hosts file

sudo nano /etc/hostname

3. Next, modify the hosts files as well using nano editor:

sudo nano /etc/hosts

And reboot the system

reboot

3. Installing Postfix on Ubuntu

First, update and upgrade the system using the following command

sudo apt-get update && sudo apt-get upgrade

Now Install all required Dependencies using the following command

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

During installation, you will be prompted to provide additional information and configure Postfix. First, it will ask to select the best suitable server settings. Since we are going to use SMTP, select the Internet Site option from the list and hit the [Enter] button

Next, you will be asked to set the fully qualified domain name FQDN. This is nothing but the domain extension that you want to receive the email from.

We should generally set this same as the hostname. Hence, fill in the same and hit on the [Enter] button.

You can always reconfigure the Postfix using the below dpkg command:

dpkg-reconfigure postfix

4. Configure Postfix with Gmail SMTP

Now that your Postfix is installed, head over to configure Postfix to use Gmail for SMTP relay.

Open the Postfix configuration file main.cf using located in /etc/postfix/ directory.

sudo nano /etc/postfix/main.cf

Scroll to the bottom to find the relayhost = option and set it to Gmail SMTP server

relayhost = [smtp.gmail.com]:587

Next, add a few lines at the end of all other existing code to enable secure authentication and read the hashed password for SMTP.

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

We will add the password credentials into sasl_password for authenticating the encrypted SMTP connection.

sudo nano /etc/postfix/sasl/sasl_passwd

Add the Gmail password in the below format in the sasl_passwd file:

[smtp.gmail.com]:587 My-Mail-Address@gmail.com:My_Password

Mention My-Mail-Address as your mail id and My_Password as mail password

Now Convert the sasl_passwd file into a database file, and delete the original file from the server. We can use the postmap command for converting.

sudo postmap /etc/postfix/sasl_passwd

This will create a sasl_passwd.db file in the same location.

And now change the security and ownership of the password file to restrict root user access and read-write only.

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

Now we are going to create the certificate.

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

There should now be a certificate file called cacert.pem in /etc/postfix

Finally, restart the postfix service using the below command to make the changes permanent.

sudo service postfix restart

At last change the setting in Google Account to allow less secured non-Google apps to use authentication to send emails via SMTP on your behalf.

https://myaccount.google.com/u/3/security

5. Sending a Test Email

Once you’ve restarted the Postfix post-configuration, just try sending a test email using the below command.

echo “Message” | mail -s “Subject” Sender-Mail-Id@gmail.com

Change the following parameters as required

Message

Subject

Sender-Mail-Id@gmail.com

To check if any mails are in queue use follow the command

sudo postqueue -p

6. Received Mail POC

Here is the received mail

--

--

CSCC LABS
CSCC LABS

Written by CSCC LABS

Cybersecurity Comprehensive Coverage

No responses yet