设置 Postfix 以接收邮件
Tier: 基础版,专业版,旗舰版
Offering: 私有化部署
本文档将引导你完成在 Ubuntu 上设置一个带有 IMAP 认证的基本 Postfix 邮件服务器的步骤,用于 接收邮件。
这些说明假设你使用的是电子邮件地址 incoming@gitlab.example.com,即主机 gitlab.example.com 上的用户名 incoming。在执行示例代码片段时,请不要忘记将其更改为你的实际主机。
配置服务器防火墙
- 在服务器上打开端口 25,以便人们可以通过 SMTP 向服务器发送邮件。
- 如果邮件服务器与运行极狐GitLab 的服务器不同,请在服务器上打开端口 143,以便极狐GitLab 可以通过 IMAP 从服务器读取邮件。
安装软件包
-
如果尚未安装 postfix 软件包,请安装它:
shellsudo apt-get install postfix当询问环境时,选择 'Internet Site'。当要求确认主机名时,请确保它与 gitlab.example.com 匹配。
-
安装 mailutils 软件包。
shellsudo apt-get install mailutils
创建用户
-
为接收邮件创建一个用户。
shellsudo useradd -m -s /bin/bash incoming -
为该用户设置密码。
shellsudo passwd incoming请务必记住此密码,稍后会用到。
测试开箱即用的设置
-
连接到本地 SMTP 服务器:
shelltelnet localhost 25你应该会看到类似以下的提示:
shellTrying 127.0.0.1... Connected to localhost. Escape character is '^]'. 220 gitlab.example.com ESMTP Postfix (Ubuntu)如果收到 Connection refused 错误,请验证 postfix 是否正在运行:
shellsudo postfix status如果未运行,请启动它:
shellsudo postfix start -
向新创建的 incoming 用户发送一封测试 SMTP 的邮件,在 SMTP 提示符下输入以下内容:
plaintext1ehlo localhost 2mail from: root@localhost 3rcpt to: incoming@localhost 4data 5Subject: 回复:某个议题 6 7听起来不错! 8. 9quit如果在输入 rcpt to: incoming@localhost 后收到错误,则你的 Postfix my_network 配置不正确。错误信息会显示 'Temporary lookup failure'。请参阅 配置 Postfix 接收来自互联网的邮件。
-
检查 incoming 用户是否收到了邮件:
shellsu - incoming mail你应该会看到类似以下的输出:
plaintext"/var/mail/incoming": 1 message 1 unread >U 1 root@localhost 59/2842 回复:某个议题退出邮件程序:
shellq -
退出 incoming 账户,返回 root 身份:
shelllogout
配置 Postfix 使用 Maildir 格式邮箱
Courier(我们稍后会安装以添加 IMAP 认证)要求邮箱采用 Maildir 格式,而不是 mbox。
-
配置 Postfix 使用 Maildir 格式邮箱:
shellsudo postconf -e "home_mailbox = Maildir/" -
重启 Postfix:
shellsudo /etc/init.d/postfix restart -
测试新设置:
-
按照 测试开箱即用的设置 的步骤 1 和 2 操作。
-
检查 incoming 用户是否收到了邮件:
shellsu - incoming MAIL=/home/incoming/Maildir mail你应该会看到类似以下的输出:
plaintext"/home/incoming/Maildir": 1 message 1 unread >U 1 root@localhost 59/2842 回复:某个议题退出邮件程序:
shellq
如果 mail 返回错误 Maildir: Is a directory,则你的 mail 版本不支持 Maildir 格式邮箱。通过运行 sudo apt-get install heirloom-mailx 安装 heirloom-mailx。然后,再次尝试上述步骤,将 mail 命令替换为 heirloom-mailx。
-
-
退出 incoming 账户,返回 root 身份:
shelllogout
安装 Courier IMAP 服务器
-
安装 courier-imap 软件包:
shellsudo apt-get install courier-imapUbuntu 24.04 没有 courier-imap 软件包。更多信息,请参阅 Ubuntu bug 2071662。
安装 courier-imap 后,启动 imapd:
shellimapd start -
安装后 courier-authdaemon 不会自动启动。没有它,IMAP 认证会失败:
shellsudo service courier-authdaemon start你还可以配置 courier-authdaemon 在启动时自动运行:
shellsudo systemctl enable courier-authdaemon
配置 Postfix 接收来自互联网的邮件
-
让 Postfix 知道它应视为本地的域:
shellsudo postconf -e "mydestination = gitlab.example.com, localhost.localdomain, localhost" -
让 Postfix 知道它应视为局域网一部分的 IP:
假设 192.168.1.0/24 是你的本地局域网。如果你在同一本地网络中没有其他机器,可以安全地跳过此步骤。
shellsudo postconf -e "mynetworks = 127.0.0.0/8, 192.168.1.0/24" -
配置 Postfix 在所有接口上接收邮件,包括互联网:
shellsudo postconf -e "inet_interfaces = all" -
配置 Postfix 使用 + 作为子地址分隔符:
shellsudo postconf -e "recipient_delimiter = +" -
重启 Postfix:
shellsudo service postfix restart
测试最终设置
-
测试新设置下的 SMTP:
-
连接到 SMTP 服务器:
shelltelnet gitlab.example.com 25你应该会看到类似以下的提示:
shellTrying 123.123.123.123... Connected to gitlab.example.com. Escape character is '^]'. 220 gitlab.example.com ESMTP Postfix (Ubuntu)如果收到 Connection refused 错误,请确保防火墙已设置为允许端口 25 的入站流量。
-
向 incoming 用户发送一封测试 SMTP 的邮件,在 SMTP 提示符下输入以下内容:
plaintext1ehlo gitlab.example.com 2mail from: root@gitlab.example.com 3rcpt to: incoming@gitlab.example.com 4data 5Subject: 回复:某个议题 6 7听起来不错! 8. 9quit -
检查 incoming 用户是否收到了邮件:
shellsu - incoming MAIL=/home/incoming/Maildir mail你应该会看到类似以下的输出:
plaintext"/home/incoming/Maildir": 1 message 1 unread >U 1 root@gitlab.example.com 59/2842 回复:某个议题退出邮件程序:
shellq -
退出 incoming 账户,返回 root 身份:
shelllogout
-
-
测试新设置下的 IMAP:
-
连接到 IMAP 服务器:
shelltelnet gitlab.example.com 143你应该会看到类似以下的提示:
shellTrying 123.123.123.123... Connected to mail.gitlab.example.com. Escape character is '^]'. - OK [CAPABILITY IMAP4rev1 UIDPLUS CHILDREN NAMESPACE THREAD=ORDEREDSUBJECT THREAD=REFERENCES SORT QUOTA IDLE ACL ACL2=UNION] Courier-IMAP ready. Copyright 1998-2011 Double Precision, Inc. See COPYING for distribution information. -
以 incoming 用户身份登录以测试 IMAP,在 IMAP 提示符下输入以下内容:
plaintexta login incoming PASSWORD将 PASSWORD 替换为你之前为 incoming 用户设置的密码。
你应该会看到类似以下的输出:
plaintexta OK LOGIN Ok. -
断开与 IMAP 服务器的连接:
shella logout
-
完成
如果所有测试都成功,Postfix 已全部设置完毕并准备好接收邮件!继续按照 接收邮件 指南配置极狐GitLab。
本文档改编自 https://help.ubuntu.com/community/PostfixBasicSetupHowto,由 Ubuntu 文档维基的贡献者提供。