SMTP 发信
smtp;MTA 直觉。
30 分钟
SMTP:邮件发送协议
SMTP(Simple Mail Transfer Protocol)负责 从发件人到收件人邮件服务器 的投递。运维场景包括:监控告警邮件、应用事务通知(注册验证、密码重置)、批量营销(需合规)。本课衔接 networking-essentials 端口知识,MX 记录见 dns-essentials,服务器操作见 linux-essentials,配置管理见 git-essentials。
学习目标
- 说出 SMTP 常用端口 25 / 587 / 465 的区别与使用场景
- 用
swaks或telnet手工测试 SMTP 会话 - 理解 MTA → MTA 中继 与应用发信(Submission)的差异
SMTP 端口对照
| 端口 | 名称 | 用途 | 加密 |
|---|---|---|---|
| 25 | SMTP | 服务器间 MTA 中继 | 通常明文,部分 STARTTLS |
| 587 | Submission | 客户端/应用 提交 邮件 | STARTTLS 或 TLS |
| 465 | SMTPS | 隐式 TLS(legacy) | 全程 SSL/TLS |
应用发信(推荐路径)
App ──587 STARTTLS──► Postfix/relay ──25──► 收件方 MX
25 端口 常被云厂商封禁(防 spam);应用应走 587 + 认证。
手工测试 SMTP
# 安装 swaks(SMTP 测试瑞士军刀)
# Debian/Ubuntu: sudo apt install swaks
# 通过 Submission 端口发测试信
swaks --to ops@example.com \
--from noreply@example.com \
--server smtp.example.com:587 \
--auth LOGIN \
--auth-user noreply@example.com \
--auth-password 'secret' \
--tls
# openssl 查看 STARTTLS 握手
openssl s_client -connect smtp.example.com:587 -starttls smtp
# 然后输入:
# EHLO localhost
# AUTH LOGIN
# (base64 用户名/密码)
# MAIL FROM:<noreply@example.com>
# RCPT TO:<ops@example.com>
# DATA
# Subject: test
#
# body
# .
# QUIT
Postfix 最小发信配置(示意)
/etc/postfix/main.cf:
myhostname = mail.example.com
mydomain = example.com
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost
relayhost = [smtp.sendgrid.net]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_security_level = encrypt