# 安裝核心包(CentOS/RHEL 5/6/7 通用)
yum install -y sendmail sendmail-cf m4 dovecot cyrus-sasl
# 啟動服務並設置開機自啟
service sendmail start
service saslauthd start
service dovecot start
chkconfig sendmail on
chkconfig saslauthd on
chkconfig dovecot on
useradd mailuser && echo "Pass123" | passwd --stdin mailusermail -s "Test" mailuser@localhost <<< "Body"
su - mailuser -c "mail"
mailq 或 sendmail -bp| 文件 | 角色 | 關鍵修改點 |
|---|---|---|
/etc/mail/sendmail.mc | 主配置源文件(M4 宏) | 修改 DAEMON_OPTIONS 監聽 0.0.0.0:25;啟用 FEATURE(\`access_db')、FEATURE(\`virtusertable') 等 |
/etc/mail/access | 訪問控制/中繼授權 | 格式:域名/IP RELAY/OK/DISCARD;修改後需 makemap hash /etc/mail/access < /etc/mail/access |
/etc/mail/local-host-names | 本地郵件域列表 | 每行一個域名(如 example.com),郵件將本地投遞而非轉發 |
/etc/mail/virtusertable | 虛擬用戶映射 | user@domain.com local_user;修改後 makemap hash ... |
/etc/dovecot/dovecot.conf | IMAP/POP3 服務 | 配置 mail_location = maildir:~/Maildir、啟用 auth_mechanisms = plain login |
# /etc/mail/sendmail.mc 第 116 行左右
dnl DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl
DAEMON_OPTIONS(`Port=smtp,Addr=0.0.0.0, Name=MTA')dnl
重新生成 sendmail.cf 並重啟:
m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
service sendmail restart
# /etc/sysconfig/network
HOSTNAME=mail.example.com
# /etc/hosts
127.0.0.1 mail.example.com localhost
<公網IP> mail.example.com
# /etc/resolv.conf
nameserver <本機或內網DNS>
# 確保 mail.example.com A 記錄、MX 記錄、PTR 記錄(反向解析)均正確
# /etc/mail/access
example.com RELAY
192.168.1.0/24 RELAY
10.0.0.0/8 OK
spammer.com DISCARD
# 生效
makemap hash /etc/mail/access < /etc/mail/access
service sendmail restart
# /etc/mail/sendmail.mc 追加
TRUST_AUTH_MECH(`LOGIN PLAIN')dnl
define(`confAUTH_MECHANISMS', `LOGIN PLAIN')dnl
define(`confAUTH_OPTIONS', `A p')dnl
DAEMON_OPTIONS(`Port=smtps, Name=TLSMTA, M=s')dnl
# 生成證書(自簽或申請正式證書)
openssl req -new -x509 -days 3650 -nodes -out /etc/mail/cert.pem -keyout /etc/mail/key.pem
chmod 600 /etc/mail/key.pem
# 重新生成 cf 並重啟
m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
service sendmail restart
service saslauthd restart
named.conf 添加 forwarders { 對方IP; }; 實現 DNS 轉發。/etc/mail/access 互加 RELAY:# 163.com 服務器
163.com OK
sina.com RELAY
# sina.com 服務器
sina.com OK
163.com RELAY
生效:makemap hash ... && service sendmail restart
mail.example.com:110 (POP3) / 995 (POP3S) / 143 (IMAP) / 993 (IMAPS)mail.example.com:25 / 465 (SMTPS) / 587 (Submission)| 現象 | 排查方向 | 關鍵命令/日誌 |
|---|---|---|
| 無法發送(連接超時) | 防火牆 25/465/587 端口、ISP 封鎖 25 端口 | telnet mail.example.com 25、iptables -nL |
| 郵件進垃圾箱/被拒收 | 缺少 SPF/DKIM/DMARC、IP 信譽差、PTR 未設置 | dig txt example.com、郵件原文檢視 Authentication-Results |
| 本地用戶收不到外域郵件 | local-host-names 缺域、MX 記錄錯誤、Dovecot 未啟動 | cat /etc/mail/local-host-names、netstat -lnpt | grep dovecot |
| 發信隊列堆積 | 遠程服務器拒收、DNS 解析失敗、磁盤滿 | mailq、/var/log/maillog、df -h |
Sendmail 配置複雜、安全歷史漏洞多、性能不如 Postfix。新項目建議直接部署 Postfix + Dovecot + Roundcube,配置語法簡潔,生態成熟,社區活躍。遷移時可保留 /etc/mail/access、virtusertable 等數據文件,僅重寫主配置邏輯。