Postfix is a popular open-source mail transfer agent (MTA) used to route and deliver email on Linux systems. It provides a robust and efficient means of handling mail delivery. In this tutorial, we will show you how to configure Postfix to use Gmail’s SMTP service on Ubuntu and Debian-based systems. This setup allows you to send emails through Gmail’s infrastructure, providing better deliverability and reducing the chances of your emails being marked as spam.

Advertisement

Prerequisites

Before proceeding, ensure that you have the following:

  • A system running Ubuntu or Debian-based distribution.
  • A Gmail account or Google Workspace account with ‘Less secure apps‘ enabled.
  • Root or sudo access to the system.

Step 1: Install Postfix and Required Dependencies

First, update your system’s package index:

sudo apt update 

Next, install Postfix and the mailutils package, which provides additional utilities for handling mail:

sudo apt install postfix mailutils 

During the Postfix installation, you will be prompted to select the mail server configuration type. Choose ‘Internet Site’ and enter your fully qualified domain name (FQDN) when prompted.

Step 2: Configure Postfix to Use Gmail SMTP

Edit the Postfix configuration file /etc/postfix/main.cf using your preferred text editor:

sudo nano /etc/postfix/main.cf 

Add or modify the following lines in the configuration file:

Save and exit the file.

Step 3: Create and Configure the SASL Password File

Create a new file /etc/postfix/sasl_passwd to store your Gmail account’s credentials:

sudo nano /etc/postfix/sasl_passwd 

Add the following line, replacing your_email@example.com with your Gmail email address and your_password with your Gmail password:

Save and exit the file.

Secure the password file by changing its permissions:

sudo chmod 600 /etc/postfix/sasl_passwd 

Create a hash map of the password file for Postfix to use:

sudo postmap /etc/postfix/sasl_passwd 

Step 4: Restart Postfix and Test Email Functionality

Restart the Postfix service to apply the changes:

sudo systemctl restart postfix 

Send a test email using the mail command to ensure that Postfix is correctly configured:

echo "This is a test email." | mail -s "Test Email" recipient@example.com 

Replace recipient@example.com with a valid email address. Check the recipient’s inbox to ensure that the email was delivered successfully.

Conclusion

You have now configured Postfix to use Gmail SMTP on your Ubuntu or Debian-based system. This setup allows you to send emails through Gmail’s infrastructure, improving email deliverability and reducing the likelihood of your messages being marked as spam. Remember to regularly update your system and Postfix configuration to maintain a secure and reliable email service.

Share.
Leave A Reply


Exit mobile version