Postfix 是一款很棒的程序,它可以路由并将电子邮件传递到系统外部的帐户。目前大约 33% 的互联网邮件服务器 都在使用它。在本文中,我将解释如何使用 Postfix 通过启用双因素身份验证的 Gmail 发送邮件。
但是,在启动并运行 Postfix 之前,您需要准备一些项目。以下是如何在多种发行版上使其工作的说明。
先决条件
- 已安装的操作系统(Ubuntu/Debian/Fedora/Centos/Arch/FreeBSD/OpenSUSE)
- 具有双因素身份验证的 Google 帐户
- 可用的互联网连接
步骤 1:准备 Google
打开 Web 浏览器并登录您的 Google 帐户。登录后,点击您的图片并选择“Google 帐户”转到您的设置。点击“登录和安全”,然后向下滚动到“应用密码”。使用您的密码登录。然后您可以创建一个新的应用密码(我将我的命名为“postfix Setup”)。

记下疯狂的密码(如下所示),我将在本文中通篇使用它。

步骤 2:安装 Postfix
在配置邮件客户端之前,您需要安装它。您还必须安装 mailutils
或 mailx
实用程序,具体取决于您使用的操作系统。以下是如何为每个操作系统安装它
Debian/Ubuntu:
apt-get update && apt-get install postfix mailutils
Fedora:
dnf update && dnf install postfix mailx
Centos:
yum update && yum install postfix mailx cyrus-sasl cyrus-sasl-plain
Arch:
pacman -Sy postfix mailutils
FreeBSD:
portsnap fetch extract update
cd /usr/ports/mail/postfix
make config
在配置对话框中,选择“SASL 支持”。所有其他选项可以保持不变。
从那里:make install clean
从二进制包安装 mailx
:pkg install mailx
OpenSUSE:
zypper update && zypper install postfix mailx cyrus-sasl
步骤 3:设置 Gmail 身份验证
安装 Postfix 后,您可以设置 Gmail 身份验证。由于您已经创建了应用密码,因此您需要将其放入配置文件中并锁定它,以防止其他人看到它。幸运的是,这很容易做到
Ubuntu/Debian/Fedora/Centos/Arch/OpenSUSE:
vim /etc/postfix/sasl_passwd
添加此行
[smtp.gmail.com]:587 ben.heffron@gmail.com:thgcaypbpslnvgce
保存并关闭文件。由于您的 Gmail 密码以明文形式存储,因此请使该文件仅对 root 用户可访问,以确保额外安全。
chmod 600 /etc/postfix/sasl_passwd
FreeBSD:
vim /usr/local/etc/postfix/sasl_passwd
添加此行
[smtp.gmail.com]:587 ben.heffron@gmail.com:thgcaypbpslnvgce
保存并关闭文件。由于您的 Gmail 密码以明文形式存储,因此请使该文件仅对 root 用户可访问,以确保额外安全。
chmod 600 /usr/local/etc/postfix/sasl_passwd

步骤 4:让 Postfix 运转起来
此步骤是“重头戏”——到目前为止您所做的一切都是准备工作。
Postfix 从 main.cf
文件获取其配置,因此此文件中的设置至关重要。对于 Google,必须启用正确的 SSL 设置。
以下是您需要在 main.cf
上输入或更新的六个选项,以使其与 Gmail 配合使用(来自 SASL 自述文件)
- smtp_sasl_auth_enable 设置启用客户端身份验证。我们将在示例的第二部分配置客户端的用户名和密码信息。
- relayhost 设置强制 Postfix SMTP 将所有远程消息发送到指定的邮件服务器,而不是尝试直接将它们传递到其目的地。
- 使用 smtp_sasl_password_maps 参数,我们将 Postfix SMTP 客户端配置为将用户名和密码信息发送到邮件网关服务器。
- Postfix SMTP 客户端 SASL 安全选项使用 smtp_sasl_security_options 设置,其中包含大量选项。在这种情况下,它将为空;否则,Gmail 将无法与 Postfix 很好地配合使用。
- smtp_tls_CAfile 是一个文件,其中包含受信任的根 CA 的 CA 证书,用于签署远程 SMTP 服务器证书或中间 CA 证书。
- 来自 配置设置页面: stmp_use_tls 在远程 SMTP 服务器声明 STARTTLS 支持时使用 TLS,默认是不使用 TLS。
Ubuntu/Debian/Arch
这三个操作系统将其文件(证书和 main.cf
)保存在同一位置,因此这就是您需要放入其中的全部内容
vim /etc/postfix/main.cf
如果以下值不存在,请添加它们
relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_security_options =
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
保存并关闭文件。
Fedora/CentOS
这两个操作系统基于相同的底层,因此它们共享相同的更新。
vim /etc/postfix/main.cf
如果以下值不存在,请添加它们
relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_security_options =
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt
保存并关闭文件。
OpenSUSE
vim /etc/postfix/main.cf
如果以下值不存在,请添加它们
relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_security_options =
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_CAfile = /etc/ssl/ca-bundle.pem
保存并关闭文件。
OpenSUSE 还要求您修改 Postfix 主进程配置文件 master.cf
。打开它进行编辑
vim /etc/postfix/master.cf
取消注释读取的行
#tlsmgr unix - - n 1000? 1 tlsmg
它应该看起来像这样
tlsmgr unix - - n 1000? 1 tlsmg
保存并关闭文件。
FreeBSD
vim /usr/local/etc/postfix/main.cf
如果以下值不存在,请添加它们
relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_security_options =
smtp_sasl_password_maps = hash:/usr/local/etc/postfix/sasl_passwd
smtp_tls_CAfile = /etc/mail/certs/cacert.pem
保存并关闭文件。
步骤 5:设置密码文件
还记得您创建的密码文件吗?现在您需要使用 postmap
将其馈送到 Postfix 中。这是 mailutils
或 mailx
实用程序的一部分。
Debian、Ubuntu、Fedora、CentOS、OpenSUSE、Arch Linux
postmap /etc/postfix/sasl_passwd
FreeBSD
postmap /usr/local/etc/postfix/sasl_passwd
步骤 6:让 Postfix 运行起来
要使所有设置和配置生效,您必须重新启动 Postfix。
Debian、Ubuntu、Fedora、CentOS、OpenSUSE、Arch Linux
这些人使重新启动变得简单
systemctl restart postfix.service
FreeBSD
要在启动时启动 Postfix,请编辑 /etc/rc.conf
vim /etc/rc.conf
添加行
postfix_enable=YES
保存并关闭文件。然后运行以下命令启动 Postfix
service postfix start
步骤 7:测试它
现在是盛大的结局——是时候测试它看看它是否有效了。mail
命令是随 mailutils
或 mailx
安装的另一个工具。
echo Just testing my sendmail gmail relay" | mail -s "Sendmail gmail Relay" ben.heffron@gmail.com
这就是我用来测试我的设置的方法,然后它出现在我的 Gmail 中。

现在您可以在 Postfix 设置中使用带有双因素身份验证的 Gmail 了。
评论已关闭。