邮件服务器
概述
邮件收, 发服务器是分开的, 也就是我们需要搭建一个邮件发送服务器和一个邮件收取服务器.
本文会搭建收, 发两个服务器, 并用邮件客户端 (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 完成信任邮件的认证.
设置域名
mail.52zt.info 用 A 记录解析到邮件服务器 IP(后面的各个客户端配置的域名都写这个 A 记录的), 再把 52zt.info 用 MX 记录解析到 mail.52zt.info(这个是当遇到 ***@52zt.info 时会解析到 mail.52zt.info).
测试端口 telnet 命令也要用 mail.52zt.info, 不能用 mx 记录的 52zt.info(用这个会解析到 A 记录解析的 52zt.info).
安装软件
安装软件 postfix,dovecot,cyrus-sasl
yum -y install postfix dovecot cyrus-sasl
配置软件
配置 postfix
- vi /etc/postfix/main.cf
- # 修改以下配置, 注意下面的变量不要重复, 如果发现与原来的变量重名, 那就将原来的变量给注释掉
- # 邮件服务器的主机名
- myhostname = mail.52zt.info
- # 邮件域
- mydomain = mail.52zt.info
- # 往外发邮件的邮件域
- myorigin = $mydomain
- # 监听的网卡
- inet_interfaces = all
- inet_protocols = all
- # 服务的对象
- mydestination = $myhostname,localhost.$myhostname, localhost.$mydomain,$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_destination
- 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
- postfix check #修改保存后检查配置文件是否有错
- systemctl start postfix #开启 postfix 服务, CentOS6 用 service postfix start
- systemctl enable postfix #设置 postfix 服务开机启动, CentOS6 用 chkconfig postfix on
配置 dovecot
- vi /etc/dovecot/dovecot.conf
- # 修改以下配置
- protocols = imap pop3 lmtp
- listen = *, ::
- # 新添加以下配置
- #----------- 自定义 ------------
- !include conf.d/10-auth.conf
- ssl = no
- disable_plaintext_auth = no
- mail_location = maildir:~/Maildir
启动 dovecot
- systemctl start dovecot #CentOS6 用 service dovecot start
- systemctl enable dovecot #CentOS6 用 chkconfig dovecot on
配置 cyrus-sasl
- vi /etc/sasl2/smtpd.conf #如果是空文件, 需要自己添加
- pwcheck_method: saslauthd
- mech_list: plain login
- log_level:3
- vi /etc/sysconfig/saslauthd #修改下面配置项(本地用户认证)
- MECH=shadow
启动
- systemctl start saslauthd #CentOS6 用 service saslauthd start
- systemctl enable saslauthd #CentOS6 用 chkconfig saslauthd on
测试
yum -y install telnet-server telnet #安装 telnet 客户端
测试发送
- [[email protected] ~]# telnet localhost 25
- Trying 127.0.0.1...
- Connected to localhost.
- Escape character is '^]'.
- 220 mail.52zt.info ESMTP Postfix
- mail from:[email protected]
- 250 2.1.0 Ok
- rcpt to:[email protected]
- 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
登录邮箱
- [[email protected] ~]# telnet localhost 110
- Trying 127.0.0.1...
- Connected to localhost.
- Escape character is '^]'.
- +OK Dovecot ready.
- user autumn
- +OK
pass 密码
- +OK Logged in.
- list #列表查看邮件
- retr 1 #读取编号为 1 的邮件
- quit #退出邮箱
用 mailx 测试
安装
yum install mailx -y
使用 mailx 发送邮件
echo '测试邮件内容' | mail -s '测试主题!' [email protected]
Outlook 配置
出现问题
在起好了服务, 开放了防火墙端口, 设置了安全组的情况下. telnet localhost 25 端口通, telnet 域名 25 不通, 是因为服务监听 ip 的问题
vi /etc/postfix/main.cf
inet_interfaces=localhost 注释掉这段, 上面写了 all, 没注意这里还有个 localhost
参考:
https://www.qcmoke.site/devops/mail.html
来源: http://www.bubuko.com/infodetail-3653596.html