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 -f
2.3 配置 DNS(在域名控制台里)
2.3.1 SPF 记录:
v=spf1 include:spf.dm.aliyun.com -all
2.3.2 DKIM 记录:
在阿里云「发信域名」页面生成 TXT 记录并添加。
2.3.3 可选 DMARC:
v=DMARC1; p=none; rua=mailto:dmarc@pangruitao.com
2.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.crt
2.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授权码
2.6 生成哈希表并设置权限
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
2.7 启动与测试
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)
即表示成功(如未在收件箱,请查垃圾箱)。
2.8 常见问题对照
| 现象 | 解决 |
|---|---|
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 |
| 收不到邮件 | 查垃圾箱、确保发件人与登录账号一致(阿里云要求) |
2.9 复用模板到其他机器
- 复制以下文件到新机:
/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。
3. 完成效果
- 任意 Linux 主机都可通过阿里云 SMTP 中继发邮件;
- 可根据发件人账号自动使用不同 SMTP 密码;
- 配合 SPF/DKIM/DMARC 验证,不会进垃圾箱。
4. 体验记录
阿里云 SMTP 对邮件内容的要求非常严格。极易容易被判定为垃圾邮件,被阿里云给拒掉。判定条件也很复杂:
- 太多链接
- 太像机器人写的文章
- 单行过长
- 等等
尝试后还是暂时放弃了。转用了 resend ,目前体验还比较好。