使用 postfix 和 dovecot 快速搭建邮件服务器

在正式进行安装邮件服务器之前,我们需要对邮件服务器概念有一些基础的了解。

1. 邮件服务器类型

邮件传输代理(MTA): 这是邮件系统中的核心组件,负责邮件的传输。常见的 MTA 包括 Postfix、Exim 等,它们通过 SMTP 协议将邮件从发件人传递到收件人的邮箱服务器。

邮件投递代理(MDA): MDA 负责将邮件从 MTA 接收后投递到用户的邮箱。Dovecot、Cyrus 等是常见的 MDA。

邮件访问代理(MUA):也称为邮件客户端,是用户用来接收、发送和管理邮件的应用程序,如 Outlook、Thunderbird 等。

2. 配置 DNS 解析

配置 A 记录:

mail.example.com -> 1.2.3.4(服务器的 ip 地址)

配置 CNAME 记录:

smtp.example.com -> mail.example.com

pop.example.com -> mail.example.com

imap.example.com -> mail.example.com

配置 MX 记录:

mail.example.com -> example.com

配置 SPF 记录:

example.com -> “v=spf1 a mx -all”

配置 DMARC 记录:

_dmarc.example.com -> “v=DMARC1; p=none; rua=mailto:dmarc-reports@example.com

反向解析 PTR 记录

DMARC 记录不是必须的,但是对邮件传输遇到的问题非常有帮助。启动服务后需要创建一个账户dmarc-reports@example.com专门用来接收 DMARC 报告。

3. 准备工作

修改主机名:

1
2
3

echo 'mail.example.com' > /etc/hostname

4. 安装 postfix

1
2
3

dnf install postfix

4.1. postfix 基本配置

Postfix 的配置文件位于 /etc/postfix 文件夹。先看 main.cf 文件,有几个重要的配置。如果事先设置了正确的 hostname,那么这些配置已经自动设置好了。

myhostname

1
2
3

myhostname = mail.example.com

myhostname 让 Postfix 知道自己主机的名字。

mydomain

1
2
3

mydomain = example.com

myorigin

1
myorigin = $mydomain

myorigin 的值可以与 mydomain 相同。

在通过 Postfix 发送邮件的时候,如果 From 字段不完整,例如 From: user,Postfix 会根据 myorigin 的值将地址补全为 From: user@example.com

mynetworks

1
2
3

mynetworks = 0.0.0.0/0

mynetworks 指定了本地网络的 IP 段,默认只包含主机自己。

inet_interfaces

1
2
3

inet_interfaces = all

inet_protocols

1
2
3

inet_protocols = all

mydestination

mydestination 指定了 Postfix 在收到这些域名地址作为目标地址的邮件时,作为接收方收下邮件。

如果收到的邮件既不符合转发规则,又不符合接收规则,则会拒绝收信。

由于我希望这台服务器能接受主域名 example.com 的邮件,所以将这个配置修改为:

1
mydestination = example.com, mail.example.com, localhost.example.com, localhost

home_mailbox

1
2
3

home_mailbox = Maildir/

4.2. 修改 postfix 默认端口

有的时候 SMTP 的默认 25 端口被 vps 提供商禁用,不能用会导致无法使用邮箱功能,解决办法修改默认端口。

1、修改 postfix 的配置文件
vim /etc/postfix/master.cf

第一行直接注销:

1
#smtp inet n – n – - smtpd

或者改成:

1
smtp2 inet n – n – - smtpd

2、修改/etc/services 文件,增加 smtp2 监听端口,本文以 2525 端口为例
vim /etc/services

找到:

1
smtp 25/tcp mail

其后添加:

1
2
3
smtp2 2525/tcp mail2

smtp2 2525/udp mail2

4.3. 启动 postfix

立即启动 postfix 并设置为开机自启动

1
2
3

systemctl enable postfix

4.4. 创建邮件账号

下面我们创建两个测试账号,用于测试发送邮件过程,也即测试 postfix 是否能正常工作。

1
2
3
4
5
6
7
8
9
10
11
12
13

# 确认mail组是否存在
$ sudo cat /etc/group | grep mail
mail:x:12:postfix

# 创建用户
useradd u1 -g mail -s /sbin/nologin
useradd u2 -g mail -s /sbin/nologin

# 为用户设置密码,并记住密码,后面会用到。
passwd u1
passwd u2

4.5. 测试 postfix 是否能够正常发送邮件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

$ telnet localhost 2525
Trying ::1...
Connected to localhost.
Escape character is '^]'.
220 mail.example.com ESMTP Postfix
mail from: u1
250 2.1.0 Ok
rcpt to: u2
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
Subject: test email
this is a test
.
250 2.0.0 Ok: queued as 8B68E60424
quit
221 2.0.0 Bye
Connection closed by foreign host.

此处的 localhost 也可以修改为服务器的 ip 或域名,即远程连接 postfix, 此时需要防火墙放行 2525 端口
使用 telnet 连接邮件服务器的 2525 端口。
mail from 指令 输入发件人
rcpt to 指令输入收件人
data 指令输入内容
Subject 输入标题
. 指令结束邮件编写
quit 指令退出

如果有任何异常,可以参考日志,参考问题排查章节进行问题排查。

5. 安装 dovecot

dovecot 用于接收邮件,作为 MDA 邮件投递代理。dovecot支持imap, pop3等多种协议。

1
2
3

dnf install -y dovecot

5.1. dovecot 基本配置

修改配置文件:/etc/dovecot/dovecot.conf

1
2
3
4
5
6
7
8
9

protocols = imap pop3

listen = *, ::

base_dir = /var/run/dovecot/

#login_trusted_networks =

编辑配置文件

修改配置文件:/etc/dovecot/conf.d/10-auth.conf

1
2
3
4
5
6
7

# 开启明文认证
disable_plaintext_auth = no

# 鉴权方式添加login
auth_mechanisms = plain login

修改配置文件:/etc/dovecot/conf.d/10-mail.conf

1
2
3

# 设置邮件路径
mail_location = mbox:~/mail:INBOX=/var/mail/%u

修改配置文件:/etc/dovecot/conf.d/10-ssl.conf

1
2
3
# 先暂时设置为no, 等申请了证书再来配置ssl验证
ssl = no

5.2. 启动 dovecot

重启 dovecot:

1
2
3

systemctl restart dovecot

设置为开机自启动

1
2
3

systemctl enable dovecot

5.3. 开启防火墙

1
2
3
4
5
6
7
8
9

# 如果防火墙已经开启, 需放行2525 - smpt,
sudo firewall-cmd --add-port=2525/tcp --permanent

# 如果防火墙已经开启, 110 pop3 端口
sudo firewall-cmd --add-port=110/tcp --permanent

sudo firewall-cmd --reload

5.4. 测试 dovecot 是否能够正常接收邮件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

$ telnet mail.example.com 110
Trying xx.xx.xx.xx...
Connected to mail.example.com.
Escape character is '^]'.
+OK Dovecot ready.
user u2
+OK
pass ******
+OK Logged in.
list
+OK 1 messages:
1 252
.

可以看到 u2@example.com 邮箱收到了一封邮件。

至此一台简单的邮件发送和接收服务器创建成功,我们可以使用各种图形化的邮件客户端,例如thunderbird, firfox等邮箱客户端连接服务器。

后续我们会使用ssl加固邮件服务器,关注鹏叔的技术博客空间,给您带来更多技术分享。

6. 问题排查

Question 1:postfix 和 dovecot 的日志在哪里?

出现问题可以查看日志。

查看日志的办法有如下几种:

使用 journalctl 查看

1
2
3
4
5
6
7

# 查看与发送邮件有关的日志
journalctl -u postfix

# 查看与连接邮箱,接收邮件有关的日志
journalctl -u dovecot

查看日志文件

1
2
3

tail -fn 100 /var/log/maillog

查看 postfix queue

1
2
3
4
5
6
7

$ mailq
# 或者

$ postqueue -p


问题 1: 邮件显示已经发出,但是收件箱显示没有邮件,如何排查?

分析:

通过查看 postfix 日志,邮件已经成功发送。

1
2
3
4
5
6
7
8

Oct 12 23:07:14 VM-XIKEK6GI postfix/cleanup[2485757]: AA8A480B6F: message-id=<[email protected]
tablenet.top>
Oct 12 23:07:14 VM-XIKEK6GI postfix/qmgr[2485633]: AA8A480B6F: from=<[email protected]>, size=323, nrcpt=1 (que
ue active)
Oct 12 23:07:14 VM-XIKEK6GI postfix/local[2485758]: AA8A480B6F: to=<[email protected]>, orig_to=<u4>, relay=loc
al, delay=15, delays=15/0.01/0/0, dsn=2.0.0, status=sent (delivered to maildir)

根据日志,邮件发送到 maildir, 进入 u4 主目录 /home/u4/Maildir/new, 确实能看到相应的邮件,但是在 thunderbird 客户端收件箱始终没有邮件。

最后发现 是 dovecot 的 mail_location 没有设置正确,将其指向正确的 mail location 即可。如下:

1
2
3
4
5

# 修改配置文件:/etc/dovecot/conf.d/10-mail.conf
# 设置邮件路径
mail_location = maildir:~/Maildir

Postfix 的 home_mailbox(/etc/postfix/main.cf)配置需要与 devocot 的 mail_location(位于/etc/dovecot/conf.d/10-mail.conf)相匹配,否则会出现以上情况。

home_mailbox 默认不配置,默认 mailbox 位于/var/mail/user,如果配置为 home_mailbox = Maildir/,则 mailbox 位于 ~/user/Maildir

问题 2: 发往 google 的邮件被退信,如何处理?

1
2
3
4
5
6
7
8
9
10
11
12
13

host gmail-smtp-in.l.google.com[74.125.203.27] said:
550-5.7.25 [xx.xx.xx.xx] The IP address sending this message does not
have a 550-5.7.25 PTR record setup, or the corresponding forward DNS entry
does not 550-5.7.25 match the sending IP. As a policy, Gmail does not
accept messages 550-5.7.25 from IPs with missing PTR records. For more
information, go to 550-5.7.25
https://support.google.com/a?p=sender-guidelines-ip 550-5.7.25 To learn
more about Gmail requirements for bulk senders, visit 550 5.7.25
https://support.google.com/a?p=sender-guidelines.
d9443c01a7336-20c8bbff548si58765675ad.159 - gsmtp (in reply to end of DATA
command)

PTR 记录没有设置正确,邮件被退回。

使用如下命令测试,发现通过 ip 查询到的域名和,通过域名查询到的 ip 不一直。

1
2
3
4
5
6
7

$ dig +short -x xx.xx.xx.xx
server.example.com.

$ dig +short a server.example.com.
yy.yy.yy.yy

解决办法,设置正确的 ptr 记录即可。

7. 参考文档

Postfix 邮件服务器安装配置

2023 如何快速搭建一个属于自己的邮箱服务器

如何选择邮件服务器

最佳开源电子邮件服务器

安装和配置 Postfix

使用 postfix 和 dovecot 快速搭建邮件服务器

https://pengtech.net/linux/email_server_install.html

作者

鹏叔

发布于

2024-10-14

更新于

2024-10-14

许可协议

评论