==============设置hostname====================
# vim /etc/hostname

mail.vip.usee88.com

vim /etc/hosts 添加

mail.vip.usee88.com

# reboot

==============安装telnet======================
yum -y install telnet
==================================================================
=             安装postfix/cyrus-sasl/dovecot                     =
==================================================================

======================安装postfix=================================
yum -y install postfix*
rpm -ql postfix|more
yum -y install openssl

postconf -d | grep mail_version
postconf -a
postconf -m

chkconfig --level 2345 postfix on

/etc/init.d/postfix restart

======================安装 cyrus-sasl===============================

rpm -qa | grep sasl

yum install cyrus-sasl*

vim /etc/sysconfig/saslauthd
---------------------------------------------------------
SOCKETDIR=/var/run/saslauthd

MECH=shadow

FLAGS=
---------------------------------------------------------
vim /etc/sasl2/smtpd.conf
---------------------------------------------------------
pwcheck_method: saslauthd
mech_list: plain login

log_level: 3
saslauthd_path:/var/run/saslauthd/mux
---------------------------------------------------------

chkconfig --level 2345 saslauthd on

systemctl restart saslauthd

# 测试
useradd tUuser && echo passtUuser | passwd --stdin tUuser

testsaslauthd -u tUuser -p 'passtUuser'

查看结果，是否success
//清理
userdel tUuser;
rm -rf /home/tUuser
rm -rf /var/spool/mail/tUuser
======================安装dovecot=================================

yum -y install dovecot dovecot-devel dovecot-mysql pam-devel

dovecot --version 查看版本

chkconfig --level 2345 dovecot on

systemctl restart dovecot

---------------------------------
systemctl status postfix
systemctl status saslauthd
systemctl status dovecot
==================================================================
=             配置postfix/cyrus-sasl/dovecot                     =
==================================================================

===================配置postfix===========================
---------------------------------------------------
1、 配置postfix--- main.cf
---------------------------------------------------
find / -name 'main.cf'

cp -p /etc/postfix/main.cf /etc/postfix/main.cf.bak
cp -p /etc/postfix/master.cf /etc/postfix/master.cf.bak

查看版本：如：postfix-2.10.1-9.el7.x86_64，
后面 /usr/share/doc/postfix-2.10.1 引用版本地址

--------------------------------------------------------
创建邮件专用账号，后面其他虚拟邮件账号收发邮件，均依赖该账号
注意：vmail, gid 5000, uid 5000
--------------------------------------------------------
groupadd -g 5000 vmail
useradd -g vmail -u 5000 -s /sbin/nologin vmail
mkdir -p /home/vmail/mail/.imap/INBOX/
chown -R vmail.vmail /home/vmail/
--------------------------------------------------------
rpm -qa | grep postfix 


# vim /etc/postfix/main.cf
// /////////////////
// 详细配置 参阅 虚拟用户相关配置/postfix/main.cf
// /////////////////
---------------------------------------------------
2、 配置postfix--- master.cf
---------------------------------------------------
# vim /etc/postfix/master.cf

smtps       inet   n       -       n       -       -       smtpd
  -o smtpd_tls_wrappermode=yes

添加 如下
dovecot   unix  -       n       n       -       -       pipe
  flags=DRhu user=vmail:vmail argv=/usr/libexec/dovecot/dovecot-lda -f ${sender} -d ${recipient}

配置后，重启
/etc/init.d/postfix start
---------------------------------------------------
3、 数据库连接相关文件
---------------------------------------------------
// 安装数据库 省略

mysql -uroot -p123456q -e "create database postfix default character set utf8 collate utf8_bin;"
mysql -uroot -p123456q -e "grant all on postfix.* to 'postfix'@'%' identified by 'postfix';"
mysql -uroot -p123456q mysql -e "delete from user where user='';"
mysql -uroot -p123456q -e "flush privileges;"
mysql -upostfix -ppostfix
quit;

mkdir /etc/postfix/sql/
创建以下几个文件，内容详见 /postfix/sql/ 文件夹
vim /etc/postfix/sql/mysql_virtual_alias_maps.cf
vim /etc/postfix/sql/mysql_virtual_alias_domain_maps.cf
vim /etc/postfix/sql/mysql_virtual_alias_domain_catchall_maps.cf
vim /etc/postfix/sql/mysql_virtual_domains_maps.cf
vim /etc/postfix/sql/mysql_virtual_mailbox_maps.cf
vim /etc/postfix/sql/mysql_virtual_alias_domain_mailbox_maps.cf
vim /etc/postfix/sql/mysql_virtual_mailbox_limit_maps.cf
========================================================

==============配置dovecot======================

1、备份文件

cp -p /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf.bak
cp -p /etc/dovecot/conf.d/10-auth.conf /etc/dovecot/conf.d/10-auth.conf.bak
cp -p /etc/dovecot/conf.d/10-mail.conf /etc/dovecot/conf.d/10-mail.conf.bak
cp -p /etc/dovecot/conf.d/10-master.conf /etc/dovecot/conf.d/10-master.conf.bak
cp -p /etc/dovecot/conf.d/10-ssl.conf /etc/dovecot/conf.d/10-ssl.conf.bak
cp -p /etc/dovecot/conf.d/10-logging.conf /etc/dovecot/conf.d/10-logging.conf.bak
cp -p /etc/dovecot/conf.d/15-lda.conf /etc/dovecot/conf.d/15-lda.conf.bak

2、 配置Dovecot--- dovecot.conf

vim /etc/dovecot/dovecot.conf

#如果不使用IPv6，请修改为

protocols = imap pop lmtp
listen = *
!include conf.d/*.conf

# 添加
passdb {
  driver = sql
  args = /etc/dovecot/dovecot-sql.conf.ext
}

userdb {
  driver = static
  args = uid=5000 gid=5000 home=/home/vmail/%d/%n
}

# 添加 调试日志
auth_debug_passwords=yes
mail_debug=yes
auth_verbose=yes
auth_verbose_passwords=plain

3、 配置Dovecot--- 10-auth.conf

vim /etc/dovecot/conf.d/10-auth.conf

# 明文授权
disable_plaintext_auth = no
auth_mechanisms = plain login cram-md5
!include auth-system.conf.ext

4、 启用ssl认证 (启用ssl：nginx-key即可)
vim /etc/dovecot/conf.d/10-ssl.conf

ssl = yes
ssl_cert = </etc/pki/tls/certs/mail.vip.usee88.com.pem
ssl_key = </etc/pki/tls/certs/mail.vip.usee88.com.key

5、 配置Dovecot--- 10-mail.conf
vim /etc/dovecot/conf.d/10-mail.conf

mail_location = maildir:/home/vmail/%d/%n/Maildir
first_valid_uid = 5000
first_valid_gid = 5000
mbox_write_locks = fcntl

6、启用dovecot的日志，如下：（注意这两个文件权限，设置777）
vim /etc/dovecot/conf.d/10-logging.conf

info_log_path = /var/log/dovecot_info.log
debug_log_path = /var/log/dovecot_dubug.log

7、 配置Dovecot--- 10-master.conf

vi /etc/dovecot/conf.d/10-master.conf

service imap-login {
  inet_listener imap {
    port = 143
  }
}

service pop3-login {
  inet_listener pop3 {
    port = 110
  }
}

service auth {
  unix_listener auth-userdb {
    mode = 0600
    user = vmail
    group = vmail
  }

  # Postfix smtp-auth
  unix_listener /var/spool/postfix/private/auth {
    mode = 0666
    user = postfix
  }
}

8、修改15-lda.conf文件
vim /etc/dovecot/conf.d/15-lda.conf
postmaster_address = postmaster@vip.usee88.com

9、添加dovecot-sql.conf.ext文件

vim /etc/dovecot/dovecot-sql.conf.ext

driver = mysql
connect = host=localhost dbname=postfix user=postfix password=postfix
default_pass_scheme = MD5-CRYPT
password_query = SELECT username AS user,password FROM mailbox WHERE username = '%u' AND active='1'
user_query = SELECT maildir, 5000 AS uid, 5000 AS gid, CONCAT('dict:storage=',floor(quota/1000),'proxy::quota') as quota FROM mailbox WHERE username = '%u' AND active='1'


# 然后启动服务
/etc/init.d/postfix restart
systemctl restart dovecot

10、调试

清空日志
echo ''>/var/log/maillog
tail -fn 20 /var/log/maillog
查看日志显示

另一个窗口逐句执行 
systemctl restart saslauthd
systemctl restart dovecot
systemctl restart postfix

systemctl status saslauthd
systemctl status dovecot
systemctl status postfix

==================================================================
=             postfixadmin配置                                   =
==================================================================

配置postfixadmi

vim path/to/postfixadmin/config.inc.php

$CONF['configured'] = true;

$CONF['default_language'] = 'cn';

$CONF['database_type'] = 'mysql';

$CONF['database_host'] = 'localhost';

$CONF['database_user'] = 'postfix';

$CONF['database_password'] = 'postfix';

$CONF['database_name'] = 'postfix';

$CONF['encrypt'] = 'dovecot:CRAM-MD5';

$CONF['dovecotpw'] = "/usr/bin/doveadm pw";

$CONF['default_aliases'] = array (
    'abuse' => 'abuse@vip.usee88.com',
    'hostmaster' => 'hostmaster@vip.usee88.com',
    'postmaster' => 'postmaster@vip.usee88.com',
    'webmaster' => 'webmaster@vip.usee88.com'
);

$CONF['footer_text'] = 'mail.usee88.com';

$CONF['footer_link'] = 'http://mail.vip.usee88.com';

$CONF['aliases']   = '1000';

$CONF['mailboxes'] = '1000';

$CONF['maxquota']  = '1000000';

$CONF['domain_quota_default'] = '1000000';

$CONF['quota'] = 'YES';

$CONF['domain_path'] = 'YES';

$CONF['domain_in_mailbox'] = 'NO';

$CONF['used_quotas'] = 'YES';

$CONF['fetchmail'] = 'YES';

$CONF['new_quota_table'] = 'YES';

修改 change-this-to-your.domain.tld => vip.usee88.com
修改 Return to change-this-to-your.domain.tld => vip.usee88.com

修改 $CONF['password_validation'] 项 
'/([a-zA-Z].*){3}/'     => 'password_no_characters 3',
改为
'/([a-zA-Z].*){1}/'     => 'password_no_characters 1',

// 建立默认站点
==============其他相关命令======================
邮件客户端出现错误：-ERR [SYS/PERM] Permission denied

1、目录权限
chmod 0600 /var/spool/mail/*

2、用户账号密码是否错误
vim /etc/passwd

# 创建用户
useradd user_demo
#设置密码，会要求输入两次密码
passwd user_demo

cd /etc/postfix
useradd c1688; echo 123456q | passwd -- stdin c1688;
mkdir -p /home/c1688/mail/.imap/INBOX/
chown -R c1688.c1688 /home/c1688/
配置相关文件

3、查看端口是否启用
reboot
netstat -ntl  // 核对 465 993，25端口是否启用

==============域名相关配置======================

主域名：demo.com
检验地址
https://www.kitterman.com/spf/validate.html

-------------一级域名发送邮件-------------------

pop3:	mail.demo.com
smtp:	mail.demo.com
email:	test@demo.com

添加A、MX、TXT记录

记录值	记录类型		记录值
mail 	A 			mailServerIP
@		MX 			mail.demo.com
@		TXT 		v=spf1 ip4:mailServerIP a mx -all
_dmarc	TXT			v=DMARC1;p=quarantine;rua=mailto:admin@demo.com	

-------------二级域名发送邮件-------------------

pop3:	mail.vip.usee88.com
smtp:	mail.vip.usee88.com
email:	test@vip.usee88.com

添加A、MX、TXT记录

记录值		记录类型		记录值
mail.vip 	A 			mailServerIP
vip			MX 			mail.vip.usee88.com
vip			TXT 		v=spf1 ip4:mailServerIP a mx -all
_dmarc		TXT			v=DMARC1;p=quarantine;rua=mailto:admin@vip.usee88.com