Postfix默认仅支持本地投递,发内网邮箱(如admin@internal)无需中继,只需正确配置mydestination和inet_interfaces;发公网邮箱(如user@gmail.com)必须配置relayhost+SASL+TLS,否则必然失败。

Postfix 本身不能直接发外网邮件,想用它做内部告警发送系统,必须明确两点:只发给内网邮箱(如 admin@local),且不配 relayhost;若要发到 QQ/163/Gmail 等公网邮箱,必须走中继 + TLS + SASL 认证,否则必然失败。
确认你的使用场景是“纯内网”还是“需触达公网”
这是最关键的分水岭。Postfix 默认行为是「仅本地投递」:收件人域名匹配 mydestination 才会落盘到 /var/mail/ 或通过 Dovecot 投递。一旦收件地址是 xxx@qq.com,而你又没配 relayhost,Postfix 就会直接拒信,日志里只显示 status=deferred (no route to host) 或 NOQUEUE: reject: RCPT。
常见误判点:
- 以为装了 Postfix 就能像 Windows 那样填个 SMTP 地址就发出去 —— 错,Linux 下它不是客户端,是 MTA(邮件传输代理)
- 看到
mail -s "test" admin@localhost成功,就以为mail -s "test" user@gmail.com也能成 —— 实际上后者根本不会进队列 - 在
/etc/postfix/main.cf里瞎加smtp_tls_security_level=encrypt却不配relayhost和 SASL —— 这只会让本地投递也卡住
纯内网报警:关掉 relay,只配 mydestination 和本地用户
适合 Zabbix、Logwatch、自定义脚本向 root、admin、ops@yourdomain.local 这类地址发告警。无需外网、不依赖第三方 SMTP、零认证风险。
操作要点:
- 确保
sendmail已卸载或禁用:sudo systemctl stop sendmail && sudo systemctl disable sendmail - 编辑
/etc/postfix/main.cf,确认以下几行未被注释且值合理:
myhostname = mail.internal mydomain = internal myorigin = $mydomain inet_interfaces = loopback-only mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain mynetworks = 127.0.0.0/8 [::1]/128
注意:inet_interfaces = loopback-only 比 all 更安全,避免暴露 25 端口给外网;mydestination 必须包含你要接收告警的域名,比如你想收 admin@internal,那 $mydomain 就得是 internal。
改完后运行:sudo postfix reload,再测试:echo "test body" | mail -s "test subject" admin@internal。检查 /var/mail/admin 是否有新邮件。
要发到 QQ/163/Gmail?必须配 relayhost + SASL + TLS
这不是可选项,是硬性要求。Gmail/Outlook/QQ 都拒绝未加密、无身份验证的连接。Postfix 不会自动帮你连它们的 SMTP,你得显式告诉它“往哪儿中继”以及“怎么证明你是谁”。
关键步骤缺一不可:
- 在
/etc/postfix/main.cf末尾加:relayhost = [smtp.qq.com]:587(注意方括号,防 DNS 查找失败) - 创建认证凭据文件:
sudo tee /etc/postfix/sasl_passwd <<EOF [smtp.qq.com]:587 123456789@qq.com:你的QQ授权码 EOF
- 生成哈希库:
sudo postmap /etc/postfix/sasl_passwd - 设权限(极其重要):
sudo chmod 600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db - 启用 TLS 和 SASL:
smtp_tls_security_level = encrypt、smtp_sasl_auth_enable = yes、smtp_sasl_security_options = noanonymous
漏掉 postmap 或权限不对,日志里只报 SASL authentication failed,不会提示文件读取失败;用 QQ 登录密码而非授权码,也会失败——QQ 邮箱后台必须开启「POP3/SMTP 服务」并生成授权码,不是账户密码。
mailx 发信失败时优先查这三处
很多人配完 Postfix,用 mail -s "a" b@c.com 发不出去,其实问题常不在 Postfix,而在 mailx 客户端本身。
典型表现和排查点:
-
mail命令卡住不动 → 检查/etc/mail.rc有没有错误配置,比如set smtp=指向了错误地址,或set smtp-auth-password=含空格/特殊字符未转义 - 收到退信说 “User unknown” →
mailx把你本地用户名当成了完整邮箱,比如你运行mail admin,它可能发给了admin@localhost而非admin@internal;强制写全地址:mail admin@internal - Postfix 日志里有
connect to smtp.qq.com[...]: Connection timed out→ 不是配置错,是服务器防火墙或云厂商安全组没放开出站 587 端口,或 DNS 解析失败(可试nslookup smtp.qq.com)
最稳妥的调试方式是绕过 mailx,直接用 Postfix 命令行投递:echo "body" | sendmail -v admin@internal,看输出是否进入队列(queued as XXXX)。如果这个通,说明 Postfix 本身没问题,问题一定出在 mailx 层或它的配置文件里。


















