centos7搭建邮箱服务器

概述

邮件收、发服务器是分开的,也就是我们需要搭建一个邮件发送服务器和一个邮件收取服务器。
本文会搭建收、发两个服务器,并用邮件客户端(Foxmail)做测试。

协议

协议就是定义规则,这里是邮件协议,定义邮件收发的规则,了解规则有助于理解软件的配置文件。
邮件发送协议 SMTP(Simple Mail Transfer Protocol),打开端口 25。
邮件收取协议 POP,打开端口 110;还有个常用邮件收取协议 IMOP,打开端口 143。

服务软件

Postfix
Postfix 是实现 SMTP 协议的软件,也叫做邮件发送服务器。

上面说的邮件客户端将邮件扔给它,由它对邮件进行转发,至于怎么转发,SMTP 协议制定了规则,而 Postfix 负责具体事情,我们只需要修改 Postfix 配置文件要求它按照我们的想法去做。

Dovecot
Dovecot 实现了 POP 和 IMOP 协议,也叫做邮件收取服务器。如果只搭建了 Postfix 而没有它,不好意思,你是收不到邮件的。

Sasl
Sasl登陆验证服务,在下面的介绍可以看到 Postfix 作为邮件发送服务器,不能无限制的转发任意邮件,应当只转发它信任的发件人发送的邮件,这一点体现在 Postfix 的配置文件要配置它认为安全的主机(mynetworks 参数)。但这样会显得很麻烦,Sasl 通过其它方式也可以帮助 Postfix 完成信任邮件的认证。

安装软件

1
yum -y install postfix dovecot  cyrus-sasl

配置软件

1
vi /etc/postfix/main.cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#修改以下配置,注意下面的变量不要重复,如果发现与原来的变量重名,那就将原来的变量给注释掉
#邮件服务器的主机名
myhostname = mail.52zt.info
#邮件域,@后面的域名
mydomain = 52zt.info
#往外发邮件的邮件域
myorigin = $mydomain
#监听的网卡
inet_interfaces = all
inet_protocols = all
#服务的对象
mydestination = $myhostname,$mydomain
#邮件存放的目录
home_mailbox = Maildir/

#新添加以下配置
#--------自定义(下面可以复制粘贴到文件最后面,用于设置服务器验为主,第一行设置发送附件大小)
#message_size_limit = 100000
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
mynetworks = 127.0.0.0/8
smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destinatio
  • smtpd_sasl_auth_enable = yes //开启认证
  • smtpd_sasl_security_options = noanonymous //不允许匿名发信
  • mynetworks = 127.0.0.0/8//允许的网段,如果增加本机所在网段就会出现允许不验证也能向外域发信
  • smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
  • 允许本地域以及认证成功的发信,拒绝认证失败的发信

检查并启动postfix

1
2
3
postfix check  #修改保存后检查配置文件是否有错
systemctl start postfix #开启postfix服务,CentOS6用service postfix start
systemctl enable postfix #设置postfix服务开机启动,CentOS6用chkconfig postfix on

配置dovecot

1
vi /etc/dovecot/dovecot.conf
1
2
3
4
5
6
7
8
9
10
11
12
#修改以下配置
protocols = imap pop3 lmtp
listen = *, ::

#新添加以下配置
#-----------自定义------------
!include conf.d/10-auth.conf

ssl = no
disable_plaintext_auth = no
mail_location = maildir:~/Maildir

启动dovecot

1
2
systemctl start dovecot    #CentOS6用service dovecot start
systemctl enable dovecot #CentOS6用chkconfig dovecot on

配置cyrus-sasl

1
vi /etc/sasl2/smtpd.conf    #如果是空文件,需要自己添加
1
2
3
pwcheck_method: saslauthd
mech_list: plain login
log_level:3
1
vi /etc/sysconfig/saslauthd  #修改下面配置项(本地用户认证)
1
MECH=shadow

启动

1
2
systemctl start saslauthd     #CentOS6用service saslauthd start
systemctl enable saslauthd #CentOS6用chkconfig saslauthd on

添加用户

1
2
useradd  autumn
echo 123456 | passwd --stdin autumn

测试

1
2
yum -y install telnet-server telnet    #安装telnet客户端
yum install telnet.* 安装telnet客户端

测试发送

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@mail ~]# telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 mail.52zt.info ESMTP Postfix
mail from:autumn@52zt.info
250 2.1.0 Ok
rcpt to:qy***@foxmail.com
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
subject:这是主题
this is test mail
.
250 2.0.0 Ok: queued as 6224C10263A