1. 目标
从任意 Linux 服务器(如 us.pangruitao.com)
通过阿里云邮件推送(smtpdm.aliyun.com)中继发送邮件。
适用 Debian / Ubuntu。
2. 配置流程
2.1 安装基础环境
sudo apt update
sudo apt install -y postfix mailutils rsyslog netcat-openbsd安装时 Postfix 选择 “Internet Site”,主机名随便填,后面会改。
2.2 设置主机标识
sudo hostnamectl set-hostname us.pangruitao.com
echo pangruitao.com | sudo tee /etc/mailname
sudo nano /etc/hosts确保文件中包含这一行(放在最上面):
127.0.0.1   us.pangruitao.com us localhost然后重新登录或执行:
exec bash
hostname -f2.3 配置 DNS(在域名控制台里)
2.3.1 SPF 记录:
v=spf1 include:spf.dm.aliyun.com -all2.3.2 DKIM 记录:
在阿里云「发信域名」页面生成 TXT 记录并添加。
2.3.3 可选 DMARC:
v=DMARC1; p=none; rua=mailto:dmarc@pangruitao.com2.4 创建 Postfix 配置
编辑主配置:
sudo nano /etc/postfix/main.cf在文件末尾添加(或覆盖重复行):
myhostname = us.pangruitao.com
myorigin = /etc/mailname
mydestination = localhost
relayhost = [smtpdm.aliyun.com]:465
# 按发件人区分中继与认证
smtp_sender_dependent_authentication = yes
sender_dependent_relayhost_maps = hash:/etc/postfix/sender_relay
# SASL & TLS
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_use_tls = yes
smtp_tls_security_level = encrypt
smtp_tls_wrappermode = yes
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt2.5 创建两张映射表
(请手敲邮箱,不要复制聊天窗口内容,以防“邮箱保护符”污染)
/etc/postfix/sender_relay
sudo nano /etc/postfix/sender_relay内容示例:
noreply@email.pangruitao.com [smtpdm.aliyun.com]:465
news@email.pangruitao.com    [smtpdm.aliyun.com]:465/etc/postfix/sasl_passwd
sudo nano /etc/postfix/sasl_passwd内容示例:
noreply@email.pangruitao.com noreply@email.pangruitao.com:你的noreplySMTP授权码
news@email.pangruitao.com    news@email.pangruitao.com:你的newsSMTP授权码⑥ 生成哈希表并设置权限
sudo postmap /etc/postfix/sender_relay
sudo postmap /etc/postfix/sasl_passwd
sudo chmod 600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
检查是否编译成功:
postmap -q 'noreply@email.pangruitao.com' /etc/postfix/sender_relay
# 应输出 [smtpdm.aliyun.com]:465
⑦ 启动与测试
sudo systemctl restart rsyslog postfix
发信测试(最稳)
/usr/sbin/sendmail -v -f noreply@email.pangruitao.com 306483372@qq.com <<'EOF'
From: noreply@email.pangruitao.com
To: 306483372@qq.com
Subject: Test via Aliyun (noreply)
Hello from noreply.
EOF
查看日志
sudo tail -n 100 /var/log/mail.log
出现:
status=sent (250 OK)
即表示成功(如未在收件箱,请查垃圾箱)。
⑧ 常见问题对照
| 现象 | 解决 | 
|---|---|
| Connection timed out | VPS 出口封 465 端口,改用 relayhost=[smtpdm.aliyun.com]:80 | 
| SASL authentication failed | /etc/postfix/sasl_passwd密码或邮箱错误 | 
| Relay access denied | sender_relay/sasl_passwd 键写错,未以邮箱为键 | 
| 没有日志 | 没启用 rsyslog: sudo apt install rsyslog -y | 
| 收不到邮件 | 查垃圾箱、确保发件人与登录账号一致(阿里云要求) | 
⑨ 复用模板到其他机器
- 复制以下文件到新机: /etc/mailname /etc/postfix/main.cf /etc/postfix/sender_relay* /etc/postfix/sasl_passwd*
- 修改 myhostname(如 hk.pangruitao.com)。
- 执行: sudo postmap /etc/postfix/sender_relay sudo postmap /etc/postfix/sasl_passwd sudo chmod 600 /etc/postfix/sasl_passwd* sudo systemctl restart postfix
- 按第 ⑦ 步再发一封测试信确认 OK。
✅ 完成效果
- 任意 Linux 主机都可通过阿里云 SMTP 中继发邮件;
- 可根据发件人账号自动使用不同 SMTP 密码;
- 配合 SPF/DKIM/DMARC 验证,不会进垃圾箱。