發送郵件需 **MTA (Mail Transfer Agent)** + **MUA (Mail User Agent)**。常見組合:
# 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 认证支持
# 基礎設置
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
[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
適合容器、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 郵件 (需 mailx 支援 -a 附件, -c 抄送)
cat <標題
紅色文本