Using Gmail as a relay for Postfix lets your server send emails reliably without worrying about IP reputation, blacklists or SPF configuration for outbound mail.
Prerequisites
- Google account with 2FA enabled
- App Password generated for Postfix
- Postfix installed on your server
Step 1 — Generate Gmail App Password
Google Account → Security → 2-Step Verification → App passwords → Generate new → copy the 16-character password
Step 2 — Install SASL Authentication
sudo apt install libsasl2-modules -y
Step 3 — Configure Postfix
sudo nano /etc/postfix/main.cf
# Gmail relay settings
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = encrypt
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
Step 4 — Create SASL Password File
sudo nano /etc/postfix/sasl_passwd
[smtp.gmail.com]:587 [email protected]:YOUR_APP_PASSWORD
sudo postmap /etc/postfix/sasl_passwd
sudo chmod 600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
Step 5 — Reload Postfix
sudo systemctl reload postfix
Step 6 — Test
echo "Test relay email" | mail -s "Postfix Gmail Relay Test" [email protected]
sudo tail -f /var/log/mail.log
Look for status=sent in the logs.
Alternative — SendGrid Relay
# Use SendGrid for higher volume
relayhost = [smtp.sendgrid.net]:587
# sasl_passwd:
[smtp.sendgrid.net]:587 apikey:YOUR_SENDGRID_API_KEY
Conclusion
Gmail SMTP relay is ideal for low-volume server notifications. For high-volume transactional email, use SendGrid or our managed mail server service.
Comments