Postfix 是一个标准的 MTA「Mail Transfer Agent」服务器,它负责通过 SMTP 协议管理发送到本机的邮件以及由本机发向外界的邮件;Dovecot 是一个优秀的 IMAP/POP 服务器用以接收外界发送到本机的邮件。
准备域名
实验环境为了方便直接修改hosts文件
[root@localhost ~]# vim /etc/hosts
# 添加
192.168.1.1 feiyi.com
安装postfix devecot
本文档只是搭建一个简单的邮件服务,其他功能还需自己去探索
Postfix、Dovecot 简介
Postfix
Postfix是一个标准的MTA服务器,它负责通过SMTP协议管理发送到本机的邮件以及由本机发向外界的邮件
Dovecot
Dovecot是一个优秀的IMAP/POP服务器,用以接收外界发送到本机的邮件。
安装
[root@localhost ~]# yum -y install postfix dovecot
配置Postfix
参考:配置文件文档
[root@localhost ~]# vim /etc/postfix/main.cf
myhostname = server.feiyi.com
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
myorigin = $mydomain
mynetworks = 127.0.0.0/8
inet_interfaces = all
inet_protocols = all
home_mailbox = Maildir/
# 末尾添加
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
broken_sasl_auth_clients = yes
smtpd_sasl_authenticated_header = yes
smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
smtpd_use_tls = yes
smtpd_tls_cert_file = /etc/pki/dovecot/certs/dovecot.pem
smtpd_tls_key_file = /etc/pki/dovecot/private/dovecot.pem
配置smtps
部分邮件客户端依赖于使用 465 端口提供加密连接,所以我们修改配置,允许 Postfix 使用 465 端口发送邮件。
[root@localhost ~]# vim /etc/postfix/master.cf
# 取消注释
smtps inet n - n - - smtpd
-o smtpd_tls_wrappermode=yes
(注意: -o
前要保留空格)
启动postfix
[root@localhost ~]# systemctl start postfix
[root@localhost ~]# systemctl enable postfix
Postfix 日志
Postfix 系统的日志文件在系统的这个目录下的 /var/log/maillog 文件,此文件记录了 Postfix 服务器的运行状态信息。
配置Dovecot
修改dovecot.conf
[root@localhost ~]# vim /etc/dovecot/dovecot.conf
# 末尾添加
ssl_cert = </etc/pki/dovecot/certs/dovecot.pem
ssl_key = </etc/pki/dovecot/private/dovecot.pem
protocols = imap pop3 lmtp
listen = *
mail_location = Maildir:~/Maildir
disable_plaintext_auth = no
修改10-master.conf
[root@localhost ~]# vim /etc/dovecot/conf.d/10-master.conf
# 取消注释
unix_listener /var/spool/postfix/private/auth {
mode = 0666
}
启动Dovecot
[root@localhost ~]# systemctl start dovecot
[root@localhost ~]# systemctl enable dovecot
/var/log/maillog
文件最后一行如下即成功
Apr 29 09:44:12 localhost dovecot: master: Dovecot v2.2.36 (1f10bfa63) starting up for imap, pop3, lmtp (core dumps disabled)
创建账户
该配置下邮箱账户依赖于系统用户,所以通过添加系统用户的方式创建邮箱账户。
添加发送用户
[root@localhost ~]# useradd test
[root@localhost ~]# passwd test
添加接收用户
使用root用户不能接收邮件,因为发送要在root的家目录下创建目录,没有权限,所以需要创建普通用户
[root@localhost ~]# useradd cyj
测试
切换为test用户发送邮件
[test@localhost ~]$ echo "postfix test" | mail -s "Chai" cyj@feiyi.com
查看cyj用户的家目录下
[root@localhost ~]# cd /home/cyj/Maildir/new/
[root@localhost new]# cat 1588126874.Vfd00I61c9680M997996.localhost.localdomain
Return-Path: <test@feiyi.com>
X-Original-To: cyj@feiyi.com
Delivered-To: cyj@feiyi.com
Received: by localhost.localdomain (Postfix, from userid 1001)
id EB695278782; Wed, 29 Apr 2020 10:21:14 +0800 (CST)
Date: Wed, 29 Apr 2020 10:21:14 +0800
To: cyj@feiyi.com
Subject: Chai
User-Agent: Heirloom mailx 12.5 7/5/10
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Message-Id: <20200429022114.EB695278782@server.feiyi.com>
From: test@feiyi.com
postfix test
成功~