在 Ubuntu 上“测试邮件服务器”通常分为几类场景:本地能否发信、SMTP 服务是否正常、接收邮件是否正常、以及对外投递是否被拦截。下面按从简单到系统的方式说明。
常见邮件服务器:
dpkg -l | grep postfix
systemctl status postfix
如果没有安装:
sudo apt update
sudo apt install postfix
mail 命令echo "test body" | mail -s "test subject" user@localhost
查看是否收到:
mail
或查看邮件目录:
ls /var/mail/
sendmail 测试echo "Subject: test" | sendmail you@example.com
sudo apt install telnet
# 或
sudo apt install netcat-openbsd
telnet localhost 25
成功会看到:
220 mail.example.com ESMTP Postfix
HELO localhost
MAIL FROM: test@localhost
RCPT TO: user@localhost
DATA
Subject: telnet test
hello world
.
QUIT
如果每一步都返回 250 OK,说明 SMTP 正常。
echo "test" | mail -s "ubuntu mail test" you@gmail.com
tail -f /var/log/mail.log
重点关注:
status=sentstatus=bouncedrelay=(是否向外中继)例如:
user@yourdomain.com
tail -f /var/log/mail.log
mail
邮件服务器必须有正确 DNS,否则外部收不到或被判垃圾邮件。
dig MX yourdomain.com
dig -x 你的服务器IP
sudo apt install swaks
swaks --to you@example.com --from test@yourdomain.com --server localhost
| 问题 | 可能原因 |
|---|---|
| 能发不能收 | MX 记录错误 |
| 能收不能发 | 被 ISP 封 25 端口 |
| 进垃圾箱 | 缺 SPF/DKIM |
| 连接超时 | 防火墙未开放 25 |
如果你愿意,可以告诉我:
我可以直接给你一套针对性测试命令。