postfix是mta负责发信和路由,dovecot是mda提供imap/pop3收信服务,二者必须协同配置路径、协议、用户认证及端口(25/143/993),并确保dns、防火墙、selinux等底层环境就绪,才能实现全功能邮件系统。

mailx 不是邮件服务器,它只是个命令行邮件客户端(MUA),不能收信、不能监听 SMTP 端口、不能管理用户邮箱。想搭“全功能邮件服务”,必须组合使用 MTA(如 postfix)+ MDA(如 dovecot)+ 可选的 Webmail(如 roundcube)。直接装 mailx 或只配 sendmail 单组件,连基本的“收发互通”都做不到。
为什么用 postfix 而不是 sendmail
CentOS/RHEL 7+ 和大多数现代发行版默认用 postfix 替代 sendmail:启动快、配置直观、SELinux 兼容性好、日志结构清晰。而 sendmail 的 sendmail.mc → m4 → sendmail.cf 编译流程极易出错,且默认开启本地中继,容易被滥用为垃圾邮件跳板。
-
postfix主配置文件是/etc/postfix/main.cf,改完直接postfix reload生效,无需重启服务 - 若系统已装
sendmail,必须先停用并移除:systemctl stop sendmail+systemctl disable sendmail+yum remove sendmail* - 检查当前默认 MTA:
alternatives --config mta,确保指向postfix
postfix 基础发信配置(无域名验证也能发)
仅需对外发信(比如监控告警),不需收信或用户登录,可跳过 Dovecot。关键是要绕过“本地用户限制”和“中继拒绝”。
- 编辑
/etc/postfix/main.cf,至少设置这四行:myhostname = mail.example.commydomain = example.cominet_interfaces = loopback-only(禁止监听外网,安全底线)mydestination = $myhostname, localhost.$mydomain, localhost(只投递本地用户) - 若要 relay 到腾讯企业邮等外部 SMTP,加:
relayhost = [smtp.exmail.qq.com]:587smtp_sasl_auth_enable = yessmtp_sasl_password_maps = hash:/etc/postfix/sasl_passwdsmtp_tls_security_level = encrypt - 写密码文件:
/etc/postfix/sasl_passwd内容为:[smtp.exmail.qq.com]:587 xitong-mail@eisc.cn:xxxxxx,然后运行postmap /etc/postfix/sasl_passwd生成哈希库
dovecot 必须配对 postfix 才能收信
Postfix 负责收信到系统邮箱(/var/spool/mail/)或 Maildir,Dovecot 负责把这部分数据通过 IMAP/POP3 暴露给客户端。两者不配对,用户就无法用 Outlook 或手机收信。
- 确认 Dovecot 使用的邮件存储路径与 Postfix 一致:
Postfix 设home_mailbox = Maildir/,则 Dovecot 的/etc/dovecot/conf.d/10-mail.conf中必须设mail_location = maildir:~/Maildir - 认证方式必须统一:Postfix 若用系统用户(
pam),Dovecot 的/etc/dovecot/conf.d/10-auth.conf就得启用!include auth-system.conf.ext;若用虚拟用户,则双方都要对接 SQL 或 LDAP - 端口开放检查:
ss -tlnp | grep ':143\|:993\|:25',确认dovecot监听 143/993,postfix监听 25 —— 缺一不可
测试链路时最容易卡在 DNS 和防火墙
90% 的“配置看起来全对但发不出/收不到”问题,根源不在软件本身,而在网络层。
- 发信失败常见报错:
status=deferred (connect to gmail-smtp-in.l.google.com[...]: Connection timed out)→ 检查本机是否被 ISP 屏蔽 25 端口,或云服务器安全组是否放行 outbound 25/587 - 收信失败典型现象:telnet
localhost 25成功,但外部 telnetyour-ip 25超时 → 防火墙(firewalld)未开 25 端口,或云平台安全组没配入向规则 - DNS 必须有反向解析(PTR 记录):很多公邮(Gmail、Outlook)会拒收无有效 PTR 的 IP 发来的信。可用
dig -x your-server-ip验证











