# 31.5.修改邮件传输代理

自 FreeBSD 版本 14.0 以降，[dma(8)](https://man.freebsd.org/cgi/man.cgi?query=dma\&sektion=8\&format=html) 是默认的 MTA，而在 14.0 之前，默认的 MTA 是 [sendmail(8)](https://man.freebsd.org/cgi/man.cgi?query=sendmail\&sektion=8\&format=html)。然而，系统管理员可以更改系统的 MTA。FreeBSD Ports 的 `mail` 类别提供了许多替代的 MTA。

> **警告**
>
> 如果默认的外发邮件服务被禁用，重要的是需要用替代的邮件传递系统进行替换。否则，像 [periodic(8)](https://man.freebsd.org/cgi/man.cgi?query=periodic\&sektion=8\&format=html) 这样的系统功能将无法通过电子邮件发送结果。系统的许多部分都依赖于一个功能正常的 MTA。如果应用程序在禁用后继续使用默认的二进制文件尝试发送邮件，邮件可能会进入一个不活跃的队列并永远无法发送。

## 31.5.1. 使用其他 MTA 替换 Sendmail

要完全禁用 [sendmail(8)](https://man.freebsd.org/cgi/man.cgi?query=sendmail\&sektion=8\&format=html)，执行以下命令：

```sh
# sysrc sendmail_enable="NO"
# sysrc sendmail_submit_enable="NO"
# sysrc sendmail_outbound_enable="NO"
# sysrc sendmail_msp_queue_enable="NO"
```

如果只想禁用 [sendmail(8)](https://man.freebsd.org/cgi/man.cgi?query=sendmail\&sektion=8\&format=html) 的接收邮件服务，执行以下命令：

```sh
# sysrc sendmail_enable="NO"
```

然后停止 [sendmail(8)](https://man.freebsd.org/cgi/man.cgi?query=sendmail\&sektion=8\&format=html) 服务：

```sh
# service sendmail onestop
```

由于 [sendmail(8)](https://man.freebsd.org/cgi/man.cgi?query=sendmail\&sektion=8\&format=html) 广泛存在，某些软件假定它已经安装并配置好，因此还需要进行一些额外的配置。检查 **/etc/periodic.conf** 并确保以下值设置为 `NO`。如果此文件不存在，可以创建它并添加这些条目：

```sh
daily_clean_hoststat_enable="NO"
daily_status_mail_rejects_enable="NO"
daily_status_include_submit_mailq="NO"
daily_submit_queuerun="NO"
```

接下来的步骤是安装另一个 MTA，本例中将使用 [dma(8)](https://man.freebsd.org/cgi/man.cgi?query=dma\&sektion=8\&format=html)。如上所述，FreeBSD 从版本 14.0 开始将 [dma(8)](https://man.freebsd.org/cgi/man.cgi?query=dma\&sektion=8\&format=html) 作为默认 MTA。因此，如果你使用的是早期版本，仅需要从 Port 安装它。

执行以下命令安装：

```sh
# pkg install dma
```

按照 [配置 DragonFly 邮件代理 (DMA)](https://docs.freebsd.org/en/books/handbook/mail/#configuring-dragonfly-mail-agent) 中的配置说明进行配置。

然后，将 **/etc/mail/mailer.conf** 文件中的所有条目更改为 [dma(8)](https://man.freebsd.org/cgi/man.cgi?query=dma\&sektion=8\&format=html)：

```ini
# 执行 "真实" 的 sendmail 程序，位于 /usr/libexec/sendmail/sendmail
#
# 如果安装了 dma(8)，可以在 /usr/share/examples/dma 中找到一个使用 dma(8) 的示例 mailer.conf
#
sendmail        /usr/local/libexec/dma
mailq           /usr/local/libexec/dma
newaliases      /usr/local/libexec/dma
```

> **注意**
>
> 当使用基本系统中包含的 [dma(8)](https://man.freebsd.org/cgi/man.cgi?query=dma\&sektion=8\&format=html) 版本时，路径将更改为 **/usr/libexec/dma**。

为了确保队列中的任何邮件在启动时或关机前被清空，执行以下命令：

```sh
# sysrc dma_flushq_enable="YES"
```

配置完成后，建议重启系统。重启可以确保系统正确配置，并在启动时自动启动新的 MTA。

### 31.5.2. 使用其他 MTA 替换 DragonFly 邮件代理 (DMA)

如上所述，从 FreeBSD 版本 14.0 开始，默认的 MTA 是 DMA。本例中将使用 [mail/postfix](https://cgit.freebsd.org/ports/tree/mail/postfix/) 作为替代 MTA。

在安装 [mail/postfix](https://cgit.freebsd.org/ports/tree/mail/postfix/) 之前，需要进行一些额外的配置。检查 **/etc/periodic.conf** 并确保以下值设置为 `NO`。如果此文件不存在，可以创建它并添加这些条目：

```sh
daily_clean_hoststat_enable="NO"
daily_status_mail_rejects_enable="NO"
daily_status_include_submit_mailq="NO"
daily_submit_queuerun="NO"
```

然后安装 [mail/postfix](https://cgit.freebsd.org/ports/tree/mail/postfix/)：

```sh
# pkg install postfix
```

要在系统启动时启用 [mail/postfix](https://cgit.freebsd.org/ports/tree/mail/postfix/)，执行以下命令：

```sh
# sysrc postfix_enable="YES"
```

> **技巧**
>
> 安装应用程序后阅读安装消息是一个好习惯，它提供了关于设置等有用的信息。

如果 postfix 尚未在 **/usr/local/etc/mail/mailer.conf** 中启用，执行以下命令：

```sh
mv /usr/local/etc/mail/mailer.conf /usr/local/etc/mail/mailer.conf.old
install -d /usr/local/etc/mail
install -m 0644 /usr/local/share/postfix/mailer.conf.postfix /usr/local/etc/mail/mailer.conf
```

在使用 SASL 时，确保 postfix 可以读取 sasldb 文件。这可以通过将 postfix 添加到邮件组，并使 **/usr/local/etc/sasldb**\* 文件可供邮件组读取来实现（这应该是新安装的默认设置）。

配置完成后，建议重启系统。重启可以确保系统正确配置，并在启动时自动启动新的 MTA。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://handbook.bsdcn.org/di-31-zhang-dian-zi-you-jian/31.5.-gai-bian-you-jian-chuan-shu-dai-li.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
