Greetings and welcome to this guide on how to configure Postfix To Relay emails using Gmail SMTP Server. But before we plunge in, it is necessary to understand what Postfix is!

Postfix is one of the popularly used mail transfer agent (MTA) software. This open-source tool is used to handle the routing and delivery of emails between different mail servers. In other words, it can act as an SMTP server or client when sending and receiving emails. It is also designed to be fast, secure and easy to configure. Postfix supports several authentication mechanisms, spam filtering techniques and encryption protocols.

The main functions performed by Postfix are:

  • Receiving emails: It listens for incoming emails on designated ports from remote servers or clients and accepts them.
  • Routing: Postfix identifies and examines the recipient address of each received email and determines the appropriate destination server for delivery.
  • Delivering emails: Postfix also transfers the email to the destination server using SMTP.
  • Queuing: whenever the destination server is temporarily unavailable, Postfix will queue the email and retry delivery at regular intervals until successful.
  • Filtering: It can also integrate with various filtering mechanisms, such as spam filters or virus scanners, to enhance email security.
  • Logging and Monitoring: Postfix is capable of maintaining logs of all mail transactions, which allows administrators to monitor and troubleshoot any issues.

Postfix can be configured to work with several email providers. Using Gmail as an SMTP server comes with a lot of benefits such as reliability, security, spam protection and cost-effectiveness. Follow the below steps and learn how to configure Postfix to relay emails using Gmail SMTP Server.

Step 1: Install Postfix on Linux

The first step requires us to have Postfix installed on the system. This can be done using the below guides:

To simplify we show commands used to install.

### Debian / Ubuntu ###
sudo apt update
sudo apt install mailutils

### RHEL based systems ###
sudo yum -y install postfix
sudo yum install mailx||sudo yum install s-nail

Once installed, ensure that the hostname is set.

sudo vim /etc/postfix/main.cf 

Provide the correct domain name:

myhostname = myserver.computingforgeeks.com

Enable postfix service to start at system boot.

sudo systemctl enable postfix

Step 2: Generate Google App Password for Postfix MTA

Due to the Two-Factor Authentication (2FA) features on Google, Gmail doesn’t allow connections to any applications that do not provide the second-step authentication. This is a very vital security feature for your Google account as it restricts unauthorized access. In order for Postfix to communicate with Gmail, we need to generate an app password on the Google account.

First, log in to your Gmail account and navigate to Security and search for app passwords

Configure Postfix To Relay emails using Gmail SMTP Server

Once here, create the app by selecting Mail

Configure Postfix To Relay emails using Gmail SMTP Server 1

For the device, select Other (Custom name) and provide a preferred name for your app.

Configure Postfix To Relay emails using Gmail SMTP Server 2

Now generate the app password and copy it as it will be required later.

Step 3: Configure Postfix to use Gmail SMTP Server

For Postfix to relay emails using Gmail SMTP Server, we need to make several adjustments. Provide your Gmail username and the app password as shown.

Open the file for editing:

sudo vim /etc/postfix/sasl_passwd

In the file add, your username, and password as shown:

[smtp.gmail.com]:587 [email protected]:your_generated_gmail_app_password

Here, replace your email address and the Gmail app password appropriately before running the below command to create a hash map of the password file.

sudo postmap /etc/postfix/sasl_passwd

After this, you should have a file named sasl_passwd.db in the /etc/postfix/. Next, we will secure the hashed database and email password files using the commands:

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

Step 4: Configure Postfix Relay Host with Gmail

There are more configurations to make for Postfix to Relay emails using Gmail SMTP Server. Open the file for editing:

sudo vim /etc/postfix/main.cf

We need to configure the relayhost and other parameters for authentication by adding the lines as shown:

#Set the Gmail Relay Host
relayhost = [smtp.gmail.com]:587

##At the end of the file, add these lines##
#############################
# Enable SASL authentication
smtp_sasl_auth_enable = yes

# Disallow methods that allow anonymous authentication
smtp_sasl_security_options = noanonymous

# Location of sasl_passwd
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd

Ensure Simple Authentication and Security Layer (SASL) modules are installed.

### Debian / Ubuntu ###
sudo apt update
sudo apt install libsasl2-modules

### RHEL based systems ###
sudo yum install cyrus-sasl-lib cyrus-sasl-plain

Save the changes and restart postfix:

sudo systemctl restart postfix

Step 5: Test Postfix emails Relay using Gmail SMTP Server

We can now test if Postfix is relaying emails using Gmail SMTP Server,. We will send a test email. Using the below syntax:

echo "This is a test email!" | mail -s "Test email" -r "[email protected]" [email protected]
##OR
echo "This is a test email!" | mail -s "Test email" [email protected]

The email should be received as shown:

Configure Postfix To Relay emails using Gmail SMTP Server 3

You will also have the app password used:

Configure Postfix To Relay emails using Gmail SMTP Server 4

You can also view the logs using the command:

sudo tail -f /var/log/syslog

Verdict

That is it for now! Today, we have walked through how to configure Postfix to relay emails using Gmail SMTP Server. I hope you benefitted from this detailed guide.

Interested in more?

LEAVE A REPLY

Please enter your comment!
Please enter your name here