# Postfix 收发与中继 ## 场景 内网要有个 SMTP 服务给应用发通知邮件,或者做一台邮件中继。 ## 前置条件 - 有域名和固定 IP - 25 端口出方向没被封(云主机要申请) - 知道用哪个邮箱做中继(第三方 SMTP 或自建) ## 配置 ```bash dnf install -y postfix postconf -e "myhostname = mail.example.com" postconf -e "mydomain = example.com" postconf -e "myorigin = \$mydomain" postconf -e "inet_interfaces = all" postconf -e "inet_protocols = ipv4" postconf -e "mydestination = \$myhostname, localhost.\$mydomain, localhost" postconf -e "mynetworks = 127.0.0.0/8, 10.0.0.0/8" systemctl enable --now postfix ``` 用第三方 SMTP 中继: ```bash postconf -e "relayhost = [smtp.example.com]:587" postconf -e "smtp_sasl_auth_enable = yes" postconf -e "smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd" postconf -e "smtp_sasl_security_options = noanonymous" postconf -e "smtp_tls_security_level = encrypt" echo "[smtp.example.com]:587 账号:密码" > /etc/postfix/sasl_passwd chmod 600 /etc/postfix/sasl_passwd postmap /etc/postfix/sasl_passwd systemctl reload postfix ``` ## 验证 ```bash echo "test body" | mail -s "test" ops@example.com mailq # 队列 tail -f /var/log/maillog ``` 发不出去时按顺序查:队列 → 日志 → 25 端口外连 → 对方是否拒收。 ## 回滚 ```bash systemctl disable --now postfix ``` ## 坑 - **云主机默认封 25 端口**,日志里是 `Connection timed out`。 - `mydestination` 配错,本地域邮件会在本机打转。 - 没有 PTR 记录,发出的邮件大比例进垃圾箱。 - `sasl_passwd` 改了不 `postmap`,还在用旧密码。 - 开放中继(`mynetworks` 写太宽)会被当成垃圾邮件源。 - 队列堆积时 `mailq` 会很长,先 `postsuper -d ALL` 清掉再找原因。