Postfix is a free tool used to send and receive emails on Linux systems. This guide shows you how to set up Postfix to send emails using Gmail’s SMTP service on Ubuntu or Debian systems. Since Google no longer allows “less secure apps,” we will use a Gmail App Password for secure access.
Prerequisites
Before you start, make sure you have:
- An Ubuntu or Debian-based system.
- A Gmail account with 2-Step Verification enabled.
- An App Password generated from your Google Account.
- Root or sudo access to the system.
Step 1: Create a Gmail App Password
You need to generate app password in your Gmail account. This app password will be used for sending email via Gmail STMP. The following steps will help you to generate app password:
- Go to your Google Account Security settings.
- Enable 2-Step Verification if not already enabled.
- In the “Signing in to Google” section, select App Passwords.
- Choose Mail as the app and Other as the device. Enter a name (e.g., “Postfix”).
- Click Generate to get a 16-character App Password. Save this password.
Step 2: Install Postfix and Dependencies
Update your system’s package list:
sudo apt update
Install Postfix and the mailutils package for email tools:
sudo apt install postfix mailutils
During Postfix installation, a setup screen will appear. Choose Internet Site and enter your system’s fully qualified domain name (FQDN) when asked.
Step 3: Configure Postfix for Gmail SMTP
Edit the Postfix configuration file /etc/postfix/main.cf
with a text editor:
sudo nano /etc/postfix/main.cf
Add or update these lines at the end of the file:
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 the file and exit.
Step 4: Set Up the SASL Password File
Create a file /etc/postfix/sasl_passwd
to store your Gmail credentials:
sudo nano /etc/postfix/sasl_passwd
Add this line, replacing [email protected]
with your Gmail address and your_app_password
with the App Password you generated:
[smtp.gmail.com]:587 [email protected]:your_app_password
Save and exit the file.
Secure the password file:
sudo chmod 600 /etc/postfix/sasl_passwd
Create a hash map of the password file for Postfix:
sudo postmap /etc/postfix/sasl_passwd
Step 5: Restart Postfix and Test Email
Restart the Postfix service to apply changes:
sudo systemctl restart postfix
Send a test email to check if it works:
echo "This is a test email." | mail -s "Test Email" [email protected]
Replace [email protected]
with a real email address. Check the recipient’s inbox to confirm the email arrived.
Conclusion
You have set up Postfix to send emails using Gmail’s SMTP service with an App Password. This method is secure and improves email delivery. Keep your system and Postfix updated for a reliable email service.
1 Comment
No longer valid as the less secure apps is no longer supported by GMAIL.