代码改变世界

怎样在 CentOS/RHEL 7/6 上安装和配置 Sendmail 服务器

2020-10-01 17:50  jetwill  阅读(763)  评论(0编辑  收藏  举报

在 CentOS 上,可以通过 mailx 命令 或 sendmail 命令来给因特网发送电子邮件。
关于前者的文章比较多,关于后者的文章比较少。

这里记录了我在 CentOS 7 上安装和配置 sendmail 服务器的过程。

1. 安装 Sendmail

yum install sendmail sendmail-cf m4

2. 配置 Sendmail 服务器

  • access — 允许/拒绝 其他系统使用 Sendmail 发送电邮
  • domaintable — 给 Sendmail 的域名映射
  • local-host-names — 用于给主机定义别名
  • mailertable — 定义指令,给特定域名重写路由
  • virtusertable — 指定具体域名形式的别名,允许多个虚拟的别名

2.1 在/etc/mail/sendmail.mc注释掉以下这行,允许从任何地方接受电邮。

在开头加上 dnl,即可在 sendmail.mc 注释掉一行。

dnl DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl

2.2 在sendmail.mc的‘MAILER’选项之上增加这行

FEATURE(relay_hosts_only)dnl

2.3 添加你PC的完全主机名到此文件

hostname >> /etc/mail/relay-domains

3. 重编译 Sendmail 配置

m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf

重启Sendmail

systemctl restart sendmail
or
service sendmail restart
or
/etc/init.d/sendmail restart

4. 配置基于域名的电子邮件的路由

如上所述,virtusertable用于别名,允许多个虚拟域名宿于一台机器。

  • All emails addressed to @example.com domain delivered to support@mydomain.com
  • All emails addressed to support@mydomain.com will forward to local user jack.
  • All emails addressed to @mydomain.com will forward to domain @otherdomain.com with corresponding usernames.
  • All emails addressed to @otherdomain.com will be rejected my mail server with acknowledging sender with the message
@example.com support@mydomain.com
support@mydomain.com  jack
@mydomain.com    %1@otherdomain.com
@otherdomain.com 	 error:nouser User unknow

执行以下命令更新 virtusertable.db 文件。

makemap hash /etc/mail/virtusertable < /etc/mail/virtusertable

最好重启Sendmail服务。

测试了以下命令,成功发送电邮到我的QQ邮箱(在这之前,mailx -s "test" saintchild@qq.com < mailbody 也邮件发送成功):

cat mailbody | sendmail saintchild@qq.com

参考