配置 Ubuntu 上发送 SMTP 邮件

介绍了在 Ubuntu 系统上使用 mailx 和 Postfix 发送 SMTP 邮件的配置方法。步骤包括安装 mailx 和 Postfix,配置 Postfix 通过外部 SMTP 服务器(例如 Amazon SES)发送邮件,设置 TLS 加密和 SASL 认证,生成并保护认证文件,以及如何测试邮件发送。博客还介绍了如何通过 ~/.mailrc 文件设置自定义发件人地址。

Ubuntu 上发送 SMTP 邮件,使用 mailx 和 Postfix 结合是一个常见的方式。以下是如何正确配置并通过 mailx 使用 SMTP 发送邮件的步骤。

  1. 安装 mailx 和 Postfix
    确保你已经安装了 mailx 和 Postfix。
1
2
sudo apt update
sudo apt install mailx postfix
  1. 配置 Postfix
    配置 Postfix 为通过外部 SMTP 服务器发送邮件。这里以 Gmail 为例,如果你使用其他 SMTP 服务器(例如 Amazon SES),替换相应的配置信息。

修改 /etc/postfix/main.cf

编辑 Postfix 配置文件 /etc/postfix/main.cf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 设置SMTP服务器的标识符
smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)

# 禁用本地邮件提醒(通常用于本地用户终端的邮件通知)
biff = no

# 禁止在本地发送邮件时自动添加域名后缀
append_dot_mydomain = no

# 禁用 Postfix README 文档目录
readme_directory = no

# 设置Postfix兼容级别为3.6,以确保使用最新的默认行为
compatibility_level = 3.6

# TLS证书文件路径
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem

# TLS私钥文件路径
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key

# 可选TLS安全级别,如果客户端支持TLS则使用
smtpd_tls_security_level=may

# 指定CA证书的路径,用于验证客户端证书
smtp_tls_CApath=/etc/ssl/certs

# 用于存储TLS会话缓存
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

# 允许本地网络和经过SASL验证的客户端转发邮件
smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination

# 配置主机名,通常是服务器的FQDN(完全限定域名)
myhostname = localhost.localdomain

# 指定用户别名文件,用于邮件转发
alias_maps = hash:/etc/aliases

# 配置别名数据库
alias_database = hash:/etc/aliases

# 指定邮件的源地址(发送者域名)
myorigin = /etc/mailname

# 设置本地邮件的目的地(接收者域名)
mydestination = $myhostname, toimc.com, localhost.localdomain, localhost.localdomain, localhost

# 配置邮件中继服务器(Amazon SES SMTP服务器)
relayhost = [email-smtp.us-east-1.amazonaws.com]:587

# 指定可以访问的网络范围(通常为本地网络)
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128

# 设置邮箱大小限制为0,表示不限制邮箱大小
mailbox_size_limit = 0

# 指定邮箱地址的分隔符,允许如user+info@example.com的邮箱地址格式
recipient_delimiter = +

# 监听所有网络接口,接受所有外部连接
inet_interfaces = all

# 启用IPv4和IPv6协议
inet_protocols = all

# 启用SMTP的SASL认证
smtp_sasl_auth_enable = yes

# 指定SASL认证的用户名和密码文件
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd

# 禁用匿名登录
smtp_sasl_security_options = noanonymous

# 强制使用TLS加密连接
smtp_tls_security_level = encrypt

# 记录STARTTLS的提供信息
smtp_tls_note_starttls_offer = yes

# 启用SMTP的TLS加密
smtp_use_tls = yes

# 禁用SMTPUTF8支持,避免与不支持该扩展的服务器出现兼容性问题
smtputf8_enable = no

  1. (可选)创建~/.mailrc指定发送人:
1
set from=no-reply@toimc.com
  1. 创建密码文件 vi /etc/postfix/sasl_passwd

类似下面的结构: [smtp.domain]:port username:password

例如:

1
[email-smtp.us-east-1.amazonaws.com]:587 AKIAT7WPVYWC25C2WBJW:你的SMTP密码

权限 sudo chmod 600 /etc/postfix/sasl_passwd

哈希 sudo postmap /etc/postfix/sasl_passwd

重启服务 systemctl restart postfix

  1. 测试
1
echo "Test email body" | mail -s "这是一个邮件测试" yourmail@domain.com