使用Postfix从Fedora系统中获取电子邮件

使用Postfix从Fedora系统中获取电子邮件

交流是非常重要的。你的电脑可能正试图告诉你一些重要的事情。但是,如果你没有正确配置邮件传输代理(MTA),那么你可能不会收到通知。Postfix 是一个易于配置且以强大的安全记录而闻名的 MTA。遵循以下步骤,以确保从本地服务发送的电子邮件通知将通过 Postfix MTA 路由到你的互联网电子邮件账户中。

安装软件包

使用 dnf 来安装一些必须软件包(你应该配置了 sudo,对吧?):

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> </span><span class="pun">-</span><span class="pln">i</span>
  2. <span class="com">#</span><span class="pln"> dnf install postfix mailx</span>

如果以前配置了不同的 MTA,那么你可能需要将 Postfix 设置为系统默认。使用 alternatives 命令设置系统默认 MTA:

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> alternatives </span><span class="pun">--</span><span class="pln">config mta</span>
  2. <span class="typ">There</span><span class="pln"> are </span><span class="lit">2</span><span class="pln"> programs which provide </span><span class="str">'mta'</span><span class="pun">.</span>
  3. <span class="pln"> </span><span class="typ">Selection</span><span class="pln"> </span><span class="typ">Command</span>
  4. <span class="pun">*+</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">sbin</span><span class="pun">/</span><span class="pln">sendmail</span><span class="pun">.</span><span class="pln">sendmail</span>
  5. <span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">sbin</span><span class="pun">/</span><span class="pln">sendmail</span><span class="pun">.</span><span class="pln">postfix</span>
  6. <span class="typ">Enter</span><span class="pln"> to keep the current selection</span><span class="pun">[+],</span><span class="pln"> </span><span class="kwd">or</span><span class="pln"> type selection number</span><span class="pun">:</span><span class="pln"> </span><span class="lit">2</span>

创建一个 password_maps 文件

你需要创建一个 Postfix 查询表条目,其中包含你要用于发送电子邮件账户的地址和密码:

  1. <span class="com">#</span><span class="pln"> MY_EMAIL_ADDRESS</span><span class="pun">=</span><span class="pln">glb@gmail</span><span class="pun">.</span><span class="pln">com</span>
  2. <span class="com">#</span><span class="pln"> MY_EMAIL_PASSWORD</span><span class="pun">=</span><span class="pln">abcdefghijklmnop</span>
  3. <span class="com">#</span><span class="pln"> MY_SMTP_SERVER</span><span class="pun">=</span><span class="pln">smtp</span><span class="pun">.</span><span class="pln">gmail</span><span class="pun">.</span><span class="pln">com</span>
  4. <span class="com">#</span><span class="pln"> MY_SMTP_SERVER_PORT</span><span class="pun">=</span><span class="lit">587</span>
  5. <span class="com">#</span><span class="pln"> </span><span class="kwd">echo</span><span class="pln"> </span><span class="str">"[$MY_SMTP_SERVER]:$MY_SMTP_SERVER_PORT $MY_EMAIL_ADDRESS:$MY_EMAIL_PASSWORD"</span><span class="pln"> </span><span class="pun">>></span><span class="pln"> </span><span class="str">/etc/</span><span class="pln">postfix</span><span class="pun">/</span><span class="pln">password_maps</span>
  6. <span class="com">#</span><span class="pln"> </span><span class="kwd">chmod</span><span class="pln"> </span><span class="lit">600</span><span class="pln"> </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">postfix</span><span class="pun">/</span><span class="pln">password_maps</span>
  7. <span class="com">#</span><span class="pln"> </span><span class="kwd">unset</span><span class="pln"> MY_EMAIL_PASSWORD</span>
  8. <span class="com">#</span><span class="pln"> history </span><span class="pun">-</span><span class="pln">c</span>

如果你使用的是 Gmail 账户,那么你需要为 Postfix 配置一个“应用程序密码”而不是使用你的 Gmail 密码。有关配置应用程序密码的说明,参阅“使用应用程序密码登录”。

接下来,你必须对 Postfix 查询表运行 postmap 命令,以创建或更新 Postfix 实际使用的文件的散列版本:

  1. <span class="com">#</span><span class="pln"> postmap </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">postfix</span><span class="pun">/</span><span class="pln">password_maps</span>

散列后的版本将具有相同的文件名,但后缀为 .db

更新 main.cf 文件

更新 Postfix 的 main.cf 配置文件,以引用刚刚创建 Postfix 查询表。编辑该文件并添加以下行:

  1. <span class="pln">relayhost </span><span class="pun">=</span><span class="pln"> smtp</span><span class="pun">.</span><span class="pln">gmail</span><span class="pun">.</span><span class="pln">com</span><span class="pun">:</span><span class="lit">587</span>
  2. <span class="pln">smtp_tls_security_level </span><span class="pun">=</span><span class="pln"> verify</span>
  3. <span class="pln">smtp_tls_mandatory_ciphers </span><span class="pun">=</span><span class="pln"> high</span>
  4. <span class="pln">smtp_tls_verify_cert_match </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">hostname</span>
  5. <span class="pln">smtp_sasl_auth_enable </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">yes</span>
  6. <span class="pln">smtp_sasl_security_options </span><span class="pun">=</span><span class="pln"> noanonymous</span>
  7. <span class="pln">smtp_sasl_password_maps </span><span class="pun">=</span><span class="pln"> hash</span><span class="pun">:</span><span class="str">/etc/</span><span class="pln">postfix</span><span class="pun">/</span><span class="pln">password_maps</span>

这里假设你使用 Gmail 作为 relayhost 设置,但是你可以用正确的主机名和端口替换系统应该将邮件发送到的邮件主机。

有关上述配置选项的最新详细信息,参考 man 帮助:

  1. <span class="pln">$ </span><span class="kwd">man</span><span class="pln"> postconf</span><span class="pun">.</span><span class="lit">5</span>

启用、启动和测试 Postfix

更新 main.cf 文件后,启用并启动 Postfix 服务:

  1. <span class="com">#</span><span class="pln"> </span><span class="kwd">systemctl</span><span class="pln"> enable </span><span class="pun">--</span><span class="pln">now postfix</span><span class="pun">.</span><span class="pln">service</span>

然后,你可以使用 exit 命令或 Ctrl+D 以 root 身份退出 sudo 会话。你现在应该能够使用 mail 命令测试你的配置:

  1. <span class="pln">$ </span><span class="kwd">echo</span><span class="pln"> </span><span class="str">'It worked!'</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> mail </span><span class="pun">-</span><span class="pln">s </span><span class="str">"Test: $(date)"</span><span class="pln"> glb@gmail</span><span class="pun">.</span><span class="pln">com</span>

更新服务

如果你安装了像 logwatchmdadmfail2banapcupsdcertwatch 这样的服务,你现在可以更新它们的配置,以便它们的电子邮件通知转到你的 Internet 电子邮件地址。

另外,你可能希望将发送到本地系统 root 账户的所有电子邮件都转到互联网电子邮件地址中,将以下行添加到系统的 /etc/alises 文件中(你需要使用 sudo 编辑此文件,或首先切换到 root 账户):

  1. <span class="pln">root</span><span class="pun">:</span><span class="pln"> glb</span><span class="pun">+</span><span class="pln">root@gmail</span><span class="pun">.</span><span class="pln">com</span>

现在运行此命令重新读取别名:

  1. <span class="com">#</span><span class="pln"> newaliases</span>
  • 提示: 如果你使用的是 Gmail,那么你可以在用户名和 @ 符号之间添加字母数字标记,如上所示,以便更轻松地识别和过滤从计算机收到的电子邮件。

常用命令

查看邮件队列:

  1. <span class="pln">$ mailq</span>

清除队列中的所有电子邮件:

  1. <span class="com">#</span><span class="pln"> postsuper </span><span class="pun">-</span><span class="pln">d ALL</span>

过滤设置,以获得感兴趣的值:

  1. <span class="pln">$ postconf </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">grep</span><span class="pln"> </span><span class="str">"^relayhost\|^smtp_"</span>

查看 postfix/smtp 日志:

  1. <span class="pln">$ </span><span class="kwd">journalctl</span><span class="pln"> </span><span class="pun">--</span><span class="kwd">no</span><span class="pun">-</span><span class="pln">pager </span><span class="pun">-</span><span class="pln">t postfix</span><span class="pun">/</span><span class="pln">smtp</span>

相关推荐