在Ubuntu上配置Postfix使用Gmail SMTP后无法发送邮件求助
Hey there, let’s work through this problem step by step—Postfix paired with Gmail can have some hidden gotchas, but we’ll track down why your test emails aren’t going through.
1. Check Gmail’s Security Settings (Most Common Culprit)
Gmail blocks regular password logins for third-party apps by default, especially if you have 2-Step Verification enabled. Here’s what to verify:
- If you use 2-Step Verification, you must use an App Password instead of your regular Gmail password. Generate one in your Google Account > Security > App Passwords (make sure you’re signed in with the correct account, and select "Mail" + "Other (Custom name)" to create a dedicated password for Postfix).
- If you don’t use 2-Step Verification, you’ll need to enable "Less secure app access" (note: Google is phasing this out, so using App Passwords is the better long-term fix).
2. Validate Postfix Configuration Files
Double-check these critical settings in /etc/postfix/main.cf:
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
Then verify your /etc/postfix/sasl_passwd file has the correct format:
[smtp.gmail.com]:587 your-gmail-address@gmail.com:your-app-password-or-less-secure-password
After editing these files, run these commands to apply changes and set proper permissions:
postmap /etc/postfix/sasl_passwd chmod 600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db systemctl restart postfix
The 600 permission is mandatory—Postfix won’t read these files if they’re accessible to other users.
3. Check Postfix Status and Logs
Logs are your best friend for debugging. First, confirm Postfix is running:
systemctl status postfix
If it’s inactive, start it with systemctl start postfix. Then monitor the mail log in real-time while sending a test email:
tail -f /var/log/mail.log
Now send a test email again, and look for error messages like:
authentication failed: Indicates a wrong password/App Password, or incorrect Gmail security settings.connection timed out/connection refused: Suggests a network or firewall block on port 587.TLS handshake failed: Might point to missing CA certificates (runupdate-ca-certificatesto fix this).
4. Verify Test Email Command and Queue
Make sure you’re using a valid test command. Try this simple one:
echo "This is a test email body" | mail -s "Postfix Test" recipient@valid-email.com
Check if emails are stuck in the Postfix queue with:
postqueue -p
If you see queued messages, force a queue refresh with:
postqueue -f
5. Rule Out Firewall/Network Issues
- Check if Ubuntu’s UFW firewall is blocking outbound traffic on port 587:
ufw allow out 587/tcp - Test connectivity to Gmail’s SMTP server:
If this fails, your ISP might be blocking port 587. Try switching to port 465 instead:nc -zv smtp.gmail.com 587- Update
relayhostinmain.cfto[smtp.gmail.com]:465 - Add
smtp_tls_wrappermode = yestomain.cf - Restart Postfix and test again.
- Update
Start with checking the mail logs first—they’ll give you the most specific clue about what’s going wrong. If you spot a particular error message, we can dig deeper!
内容的提问来源于stack exchange,提问作者OrenIshShalom

