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.
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:
1 2 3 4 5 6 | relayhost = [smtp.gmail.com]:587 smtp_use_tls = yes smtp_sasl_auth_enable = yes smtp_sasl_security_options = noanonymous smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt |
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 [email protected] with your Gmail email address and your_password with your Gmail password:
1 | [smtp.gmail.com]:587 your_email@example.com:your_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" [email protected]
Replace [email protected] 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.