萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 伺服器教學

Linux 系統下配置 Postfix + Mailx 發送郵件完整教程

架構選型

發送郵件需 **MTA (Mail Transfer Agent)** + **MUA (Mail User Agent)**。常見組合:

  • Postfix + mailx (mailutils/bsd-mailx): 標準、輕量、易配置,**推薦生產環境**。
  • Sendmail:配置複雜,歷史包袱重,不推薦新部署。
  • Exim:功能強但配置晦澀。
  • msmtp / ssmtp:純客戶端,轉發給遠程 SMTP (如 Gmail、企業郵箱),適合簡單腳本發信。

方案一:Postfix 發送郵件 (本地遞投或智能主機轉發)

安裝

# Debian/Ubuntu
apt-get update && apt-get install -y postfix mailutils
# 安裝過程選擇 "Internet Site" 或 "Satellite system"

# RHEL/CentOS/Rocky
dnf install -y postfix mailx cyrus-sasl-plain  # SASL 认证支持

核心配置 (/etc/postfix/main.cf)

# 基礎設置
myhostname = mail.example.com
mydomain = example.com
myorigin = $mydomain
inet_interfaces = loopback-only  # 僅本地發送,不接收外部郵件
mydestination = $myhostname, localhost.$mydomain, localhost
mynetworks = 127.0.0.0/8 [::1]/128

# === 關鍵:智能主機轉發 (Smart Host) ===
# 所有外發郵件轉發給上游 SMTP 服務器 (企業郵箱/阿里云/SES/SendGrid)
relayhost = [smtp.exmail.qq.com]:587  # 使用提交端口 587 (STARTTLS) 或 465 (SMTPS)

# SMTP 認證
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_sasl_mechanism_filter = login, plain

# TLS 加密 (必須)
smtp_tls_security_level = encrypt
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
smtp_tls_loglevel = 1

# 防止郵件循環
mydestination = localhost
local_transport = error:local delivery disabled

認證文件 (/etc/postfix/sasl_passwd)

[smtp.exmail.qq.com]:587  your_email@domain.com:YourPasswordOrAppToken
# 執行
postmap /etc/postfix/sasl_passwd
chmod 600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db

啟動與測試

systemctl enable --now postfix
# 測試
echo "測試郵件正文" | mail -s "測試主題" -r "your_email@domain.com" recipient@domain.com

# 查看隊列
mailq
# 查看日誌
tail -f /var/log/maillog   # RHEL
tail -f /var/log/mail.log  # Debian

方案二:msmtp + mailx (極簡客戶端模式,無本地守護進程)

適合容器、Serverless、僅需發送告警郵件的場景。

# 安裝
apt-get install -y msmtp mailutils  # 或 msmtp-mta (提供 /usr/sbin/sendmail 兼容)

# 配置 ~/.msmtprc (用戶級) 或 /etc/msmtprc (系統級)
defaults
auth           on
tls            on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile        /var/log/msmtp.log

account        default
host           smtp.exmail.qq.com
port           587
from           your_email@domain.com
user           your_email@domain.com
password       YourPasswordOrAppToken

# 測試
echo "內容" | mail -s "主題" recipient@domain.com

進階:HTML 郵件、附件、批量發送

# HTML 郵件 (需 mailx 支援 -a 附件, -c 抄送)
cat <

標題

紅色文本

EOF # 發送附件 (mailutils 版 mailx 支援 -a) echo "見附件" | mail -s "報表" -a /path/to/report.pdf -r "sender@domain.com" recipient@domain.com # 批量發送 (腳本) while read email; do echo "內容" | mail -s "通知" -r "sender@domain.com" "$email" sleep 1 # 避免觸發反垃圾郵件限流 done < email_list.txt

常見問題排查

現象原因解決
Connection timed out / Network unreachable防火牆/安全組阻擋 25/587/465 端口開放出站端口,或使用云廠商提供的內網 SMTP 代理
Authentication failed密碼錯誤/未開啟 SMTP 服務/需應用專用密碼檢查郵箱設置開啟 SMTP、生成應用專用密碼 (非登錄密碼)
Relay access deniedPostfix 未授權該客戶端轉發檢查 mynetworks、smtp_sasl_auth_enable、relayhost 配置
Certificate verification failedCA 證書缺失/過期/主機名不匹配更新 ca-certificates、設置 smtp_tls_CAfile、或暫時 smtp_tls_security_level = may (不推薦)
郵件進垃圾箱/被拒收缺少 SPF/DKIM/DMARC、IP 信譽差、內容觸發規則配置域名 SPF/DKIM/DMARC 記錄、使用可信 SMTP 服務商、優化郵件內容

安全最佳實踐

  1. **禁用 25 端口開放轉發**,僅允許本地/內網提交,強制 587/465 + TLS + 認證。
  2. **定期輪換 SMTP 憑證**,使用應用專用密碼而非主密碼。
  3. **監控郵件隊列**:`mailq | wc -l` 告警積壓,`postsuper -d ALL` 清理卡死隊列。
  4. **日誌審計**:收集 `/var/log/mail.log` 至 ELK/Loki,告警發送失敗、拒收、延遲。
copyright © 萬盛學電腦網 all rights reserved