跳转到主要内容

于 2025年04月22日 摘录自 Postfix Small/Home Office Hints and Tips

概述

本文档整合了适用于"小型办公室/家庭办公室(SOHO)"场景的应用技巧,便于集中查阅。内容仅涵盖邮件发送端配置。若您的设备无法直接接收邮件(即不具备互联网域名和固定 IP 地址),则需要使用如 fetchmail 等解决方案,该内容不属于 Postfix 文档范畴。

精选内容来自:

更多详细信息请参阅 SASL_READMESTANDARD_CONFIGURATION_README 文档。

独立互联网主机上的 Postfix

在直接连接互联网的独立主机上,Postfix 应无需修改即可正常运行。通过 https://www.postfix.org/ 下载源代码安装时即为此默认配置。

使用"postconf -n"命令可查看被 main.cf 覆盖的配置项。除部分路径设置外,独立主机基本无需额外配置,BASIC_CONFIGURATION_README 文档已涵盖主要配置项:

/etc/postfix/main.cf:
    # 可选:使用 user@domainname 替代 user@hostname 作为发件地址。
    #myorigin = $mydomain
    # 可选:指定 NAT / 代理外部地址。
    #proxy_interfaces = 1.2.3.4
    # 替代方案 1:禁止转发其他主机的邮件。
    mynetworks_style = host
    relay_domains =
    # 替代方案 2:仅转发本地客户端邮件。
    # mynetworks = 192.168.1.0/28
    # relay_domains =

若适用,请同时参考 无合法互联网主机名的主机部署 章节。

无合法互联网主机名的主机部署

本节适用于通过 DHCP 或拨号获取动态 IP 地址、无固定主机名的系统。虽然 Postfix 支持在虚构主机名的设备上收发内部邮件,但向互联网发送邮件时使用虚构地址将导致收件人无法回复。事实上,越来越多的邮件服务器会拒收使用无效域名的邮件地址。

注意:以下配置与 Postfix 版本相关。执行 "postconf mail_version" 命令可查看当前版本。

方案 1:Postfix 2.2 及更高版本

Postfix 2.2 采用 generic(5) 地址映射机制,在邮件外发时将本地虚构地址替换为有效互联网地址(仅适用于外发邮件,内部邮件不受影响)。

以下为补充配置示例(需结合前文所述的基础配置):

1 /etc/postfix/main.cf:
2     smtp_generic_maps = hash:/etc/postfix/generic
3 
4 /etc/postfix/generic:
5     [email protected]             [email protected]
6     [email protected]             [email protected]
7     @localdomain.local                [email protected]

SMTP 外发邮件时:

提示:

  • 若系统使用 dbm 而非 hash 数据库,请相应替换类型。

  • 使用 postconf -m 查看支持的查询表类型。

  • 修改 generic 表后需执行 postmap /etc/postfix/generic。

方案 2:Postfix 2.1 及更早版本

早期版本需配置有效互联网地址,并通过 Postfix 将其映射回本地虚构地址。这样既可向互联网发信,也可接收发往虚构地址的邮件。

配置示例(需结合基础配置):

 1 /etc/postfix/main.cf:
 2     myhostname = hostname.localdomain
 3     mydomain = localdomain
 4 
 5     canonical_maps = hash:/etc/postfix/canonical
 6 
 7     virtual_alias_maps = hash:/etc/postfix/virtual
 8 
 9 /etc/postfix/canonical:
10     your-login-name    [email protected]
11 
12 /etc/postfix/virtual:
13     [email protected]       your-login-name

配置说明:

注意:

  • 若系统使用 dbm 而非 hash 数据库,请相应替换类型。

  • 使用 postconf -m 查看支持的查询表类型。

  • 修改 canonical 或 virtual 表后需执行相应 postmap 命令:

    • "postmap /etc/postfix/canonical"

    • "postmap /etc/postfix/virtual

启用 Postfix SMTP/LMTP 客户端的 SASL 认证

本节演示通过需要 SASL 认证的邮件网关发送邮件的典型场景。

故障排查提示:

配置分为基础设置和认证信息两部分(基本配置和用户名 / 密码信息设置): 

/etc/postfix/main.cf:
    smtp_sasl_auth_enable = yes
    smtp_tls_security_level = encrypt
    smtp_sasl_tls_security_options = noanonymous
    relayhost = [mail.isp.example]
    # 可选端口指定格式:
    # relayhost = [mail.isp.example]:submission
    smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd

参数说明:

  • smtp_sasl_auth_enable:启用客户端认证
  • smtp_tls_security_level:强制加密连接
  • smtp_sasl_tls_security_options:禁用匿名认证
  • relayhost:强制通过指定网关发信(方括号避免 MX 查询)
  • 端口587(submission)为电子邮件客户端标准端
  • Postfix SMTP 客户端与使用非标准"AUTH=method...."语法响应 EHLO 命令的 SMTP 服务器兼容;这不需要额外的 Postfix 客户端配置。
  • "smtp_tls_wrappermode= yes":Postfix SMTP 客户端支持 "wrappermode "协议,该协议使用 SMTP 服务器(Postfix 3.0 及更高版本)上的 TCP 端口 465。
  • smtp_sasl_password_maps:指定认证凭据存储位置,配置 Postfix SMTP 客户端向邮件网关服务器发送用户名和密码信息。Postfix SMTP 客户端支持多个 ISP 账户。 因此,用户名和密码存储在一个表中,该表包含每个邮件网关服务器的一个用户名 / 密码组合。

认证凭据配置:

/etc/postfix/sasl_passwd:
    # 目标服务器                    认证凭据
    [mail.isp.example]              username:password
    # 带端口格式:
    # [mail.isp.example]:submission username:password

重要事项

需将 sasl_passwd 文件权限设置为仅 root 可读写(Postfix 会以 root 权限读取)。

Postfix SMTP 客户端仍能读取 SASL 客户端密码。在放弃权限和进入可选的 chroot jail 之前,它会以用户root的身份打开文件。

配置基于发件人的 SASL 认证

(适用于 Postfix 2.3 及更高版本)

功能概述

Postfix 支持为不同发件人地址配置不同的 ISP 账户认证信息,这在以下场景中尤为实用:

  1. 同一用户需要区分工作与个人邮箱

  2. 多个用户共享同一 Postfix 服务器但使用不同邮件服务商

实现原理

系统将按照以下优先级进行查询:

  1. 首先根据发件人地址匹配 sender_dependent_relayhost_maps 中的中继服务器(relayhost 文件)

  2. 然后根据发件人地址查找 smtp_sasl_password_maps 中的认证凭据

  3. 最后回退到默认的 relayhost 和全局认证设置

典型配置示例

主配置文件

/etc/postfix/main.cf:
    smtp_sender_dependent_authentication = yes
    sender_dependent_relayhost_maps = hash:/etc/postfix/sender_relay
    smtp_sasl_auth_enable = yes
    smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
    relayhost = [mail.isp.example]
    # 另一种形式:
    # relayhost = [mail.isp.example]:submission

认证凭据文件

/etc/postfix/sasl_passwd:
    # Per-sender authentication; see also /etc/postfix/sender_relay.
    [email protected]               username1:password1
    [email protected]               username2:password2
    # 默认 relayhost 的登录信息 .
    [mail.isp.example]              username:password
    # 另一种形式:
    # [mail.isp.example]:submission username:password

中继服务器映射文件

/etc/postfix/sender_relay:
    # 每个发件人提供者;另请参阅 /etc/postfix/sasl_passwd。
    [email protected]               [mail.example.com]:submission
    [email protected]               [mail.example.net]

高级建议:

  • 可将两类信息整合到 MySQL 数据库,然后配置不同的 Postfix 查询来提取相应的信息。
  • 若系统使用 dbm 而非 hash 数据库,请相应替换类型。

  • 使用 postconf -m 查看支持的查询表类型。

  • 每次修改映射文件后必须执行:

    • postmap /etc/postfix/sasl_passwd

    • postmap /etc/postfix/sender_relay

 
Deepseek 优化版

概述

本文档整合了适用于"小型办公室/家庭办公室(SOHO)"场景的配置技巧,方便用户集中查阅。请注意,本文仅涵盖邮件发送端的配置方案。若您的设备无法直接接收邮件(即没有专属的互联网域名和固定IP地址),则需要配合使用"fetchmail"等外部工具,这部分内容不属于Postfix官方文档范畴。

如需深入了解这些主题,请参阅 SASL_README STANDARD_CONFIGURATION_README 文档。

独立互联网主机上的Postfix配置

对于可直接访问互联网的独立主机,Postfix应当无需修改即可正常运行。通过 https://www.postfix.org/ 下载的Postfix源代码默认即采用这种配置方式。

使用postconf -n命令可查看被 main.cf 覆盖的配置项。除少量路径设置外,独立主机通常只需保持 BASIC_CONFIGURATION_README 文档中描述的默认配置:

/etc/postfix/main.cf:
    # 可选:以user@domainname格式而非user@hostname格式发送邮件
    #myorigin = $mydomain
    # 可选:指定NAT/代理的外部地址
    #proxy_interfaces = 1.2.3.4
    # 方案1:禁止转发其他主机的邮件
    mynetworks_style = host
    relay_domains =
    # 方案2:仅转发本地客户端的邮件
    # mynetworks = 192.168.1.0/28
    # relay_domains =

若您的环境适用,请同时参考无真实主机名环境下的Postfix配置章节。

无真实主机名环境下的Postfix配置

本节适用于没有合法互联网主机名的设备,常见于通过DHCP或拨号获取动态IP地址的系统。Postfix允许在虚构主机名的设备上收发本地邮件,但向互联网发送邮件时不能使用虚构地址,因为收件人将无法回复。实际上,越来越多的邮件服务器会拒收使用不存在域名的邮件地址。

注意:以下配置方案与Postfix版本相关。执行postconf mail_version命令可查询当前版本。

方案1:Postfix 2.2及更高版本

Postfix 2.2使用 generic(5) 地址映射机制,将本地虚构邮件地址替换为有效的互联网地址。此替换仅在邮件外发时生效,不影响同一设备上的用户间邮件往来。

以下为补充配置示例,需与本文前半部分的基础配置配合使用:

1 /etc/postfix/main.cf:
2     smtp_generic_maps = hash:/etc/postfix/generic
3 
4 /etc/postfix/generic:
5     [email protected]             [email protected]
6     [email protected]             [email protected]
7     @localdomain.local                [email protected]

通过SMTP发送邮件时的替换规则:

  • 第5行:将[email protected]替换为ISP邮箱地址
  • 第6行:将[email protected]替换为ISP邮箱地址
  • 第7行:其他本地地址替换为带+local扩展名的ISP账户(需ISP支持地址扩展功能)

若系统使用dbm而非db文件,请将hash替换为dbm。执行postconf -m可查询支持的查找表类型。

修改generic表后,请执行postmap /etc/postfix/generic使变更生效。

方案2:Postfix 2.1及更早版本

对于早期版本的Postfix,解决方案是优先使用有效互联网地址,并通过Postfix将有效地址映射到本地虚构地址。这样既能向互联网发送邮件,也能处理没有合法互联网地址的本地虚构地址邮件。

以下配置示例需与基础配置配合使用:

 1 /etc/postfix/main.cf:
 2     myhostname = hostname.localdomain
 3     mydomain = localdomain
 4 
 5     canonical_maps = hash:/etc/postfix/canonical
 6 
 7     virtual_alias_maps = hash:/etc/postfix/virtual
 8 
 9 /etc/postfix/canonical:
10     your-login-name    [email protected]
11 
12 /etc/postfix/virtual:
13     [email protected]       your-login-name

配置说明:

  • 第2-3行:替换为您的虚构主机名。注意不要使用互联网上已被注册的真实域名,建议参考RFC 2606列出的保留域名
  • 第5/9/10行:建立从"[email protected]"到"[email protected]"的映射(必需配置)
  • 第7/12/13行:将发往ISP账户的邮件在本地投递(可选但建议配置)

使用dbm替代hash(如系统支持)。执行postconf -m可查询支持的查找表类型。

修改配置后需执行:

  • postmap /etc/postfix/canonical(更新规范化映射表)
  • postmap /etc/postfix/virtual(更新虚拟别名表)

启用Postfix SMTP/LMTP客户端的SASL认证

典型应用场景:通过需要SASL认证的邮件网关服务器发送所有外发邮件。

故障排查提示:

配置分为基础设置和认证信息两部分:

/etc/postfix/main.cf:
    smtp_sasl_auth_enable = yes
    smtp_tls_security_level = encrypt
    smtp_sasl_tls_security_options = noanonymous
    relayhost = [mail.isp.example]
    # 替代格式(指定587端口):
    # relayhost = [mail.isp.example]:submission
    smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd

参数详解:

  • smtp_sasl_auth_enable 参数用于启用客户端认证功能。我们将在示例的第二部分配置具体的用户名和密码信息。
  • smtp_tls_security_level 参数确保与远程SMTP服务器建立加密连接,而smtp_sasl_tls_security_options 参数则解除对明文密码传输的限制。
  • relayhost 参数强制Postfix将所有外发邮件路由至指定邮件服务器,而非直接投递到目标地址。
  • relayhost配置中,使用"["和"]"包裹主机名可以避免Postfix查询该域名的MX(邮件交换)记录。
  • relayhost支持指定非标准TCP端口,例如[mail.isp.example]:submission表示使用587端口(专为邮件客户端设计的备用提交端口)。
  • Postfix SMTP客户端兼容使用非标准"AUTH=method...."语法响应EHLO命令的服务器,此功能无需额外配置。
  • 当设置"smtp_tls_wrappermode = yes"时,Postfix SMTP客户端支持"封装模式"协议(使用SMTP服务器的465端口,Postfix 3.0及以上版本支持)。
  • 通过smtp_sasl_password_maps参数,我们可以配置Postfix向邮件网关服务器提交认证信息。如后文所述,该功能支持为不同网关服务器配置不同的ISP账户凭证。
/etc/postfix/sasl_passwd:
    # 格式:目标服务器 [地址] 用户名:密码
    [mail.isp.example]              username:password
    # 带端口号的格式:
    # [mail.isp.example]:submission username:password

安全警告:

密码文件应存放在/etc/postfix目录,权限设置为root用户独占读写。Postfix会在降权前以root身份读取该文件。

使用规范:

  • 修改密码文件后执行postmap /etc/postfix/sasl_passwd
  • relayhost使用方括号或指定端口,密码文件中必须保持相同格式

配置发件人相关的SASL认证

Postfix 2.3+支持为不同发件人配置不同的ISP账户,适用于:

  • 同一设备区分工作和个人邮箱
  • 多用户共享Postfix服务器但使用不同ISP账户

实现原理:

  1. SMTP客户端优先按发件人地址查找SASL密码
  2. trivial-rewrite服务优先使用发件人相关的中继主机配置
/etc/postfix/main.cf配置文件:
    smtp_sender_dependent_authentication = yes  # 启用发件人相关认证
    sender_dependent_relayhost_maps = hash:/etc/postfix/sender_relay  # 发件人专用中继映射表
    smtp_sasl_auth_enable = yes  # 启用SASL认证
    smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd  # SASL密码映射表
    relayhost = [mail.isp.example]  # 默认中继邮件服务器
    # 替代格式(指定587端口):
    # relayhost = [mail.isp.example]:submission
/etc/postfix/sasl_passwd 认证配置文件:
    # 按发件人配置认证(关联配置文件:/etc/postfix/sender_relay)
    [email protected]               username1:password1  # 用户1的认证凭证
    [email protected]               username2:password2  # 用户2的认证凭证
    # 默认中继服务器登录信息
    [mail.isp.example]              username:password    # 主认证凭证
    # 替代格式(指定587端口):
    # [mail.isp.example]:submission username:password  # 带端口号的认证
/etc/postfix/sender_relay:
    # 发件人地址对应中继服务器
    [email protected]               [mail.example.com]:submission
    [email protected]               [mail.example.net]

高级技巧:

  • 如果您需要更灵活的配置,可以将两个配置文件整合到MySQL数据库中,并通过不同的Postfix查询语句来提取对应的配置信息。
  • 如果系统使用dbm格式而非db格式的数据库文件,请将配置中的hash替换为dbm。要查看Postfix支持的查询表类型,请执行命令"postconf -m"。
  • 每次修改sasl_passwd认证配置文件后,都需要执行"postmap /etc/postfix/sasl_passwd"命令使更改生效。
  • 同样地,修改sender_relay发件人中继配置后,也需要执行"postmap /etc/postfix/sender_relay"命令。