5分钟就让你拥有自己的邮件服务器!postfix+dovecot实现最简单的邮件收发

本教程本人亲自写的,是基于Centos7.9(8.X下也一样用),按教程一步一步下来,5分钟就能让你拥有自己的邮件服务器。

只有SMTP/POP3/IMAP功能,适用于资源比较差的云主机环境。客户端可以通过foxmail/outlook去收发,或者挂到网易邮箱或QQ邮箱代收。

以@fab.edu.org这个域名为例,以下脚本中出现的这个域名,请替换成自己的域名

以下脚本可能有些字符会被排版的原因吃掉,TXT格式的我放百度云盘,最好按这个去做,格式都对的
**[color=red]通过百度网盘分享的文件:postfix配置-虚拟用户-单域.txt
链接:https://pan.baidu.com/s/1SAvSIkLQn9xFGWpERRptIg?pwd=8888[/color]**

#CentOS7.9下安装postfix+dovecot实现最简单的邮件服务器功能,可以实现SMTP和POP3邮件收发

#修改主机名
hostnamectl set-hostname mail.fab.eu.org && bash

#关闭selinux
sed -i "/SELINUX/s/enforcing/disabled/" /etc/selinux/config && setenforce 0

#安装postfix和dovecot
yum -y install postfix dovecot vim net-tools

#防火墙端口放行,云主机的话放行25,110,143/tcp端口
firewall-cmd --add-service={smtp,imap,pop3} --permanent && firewall-cmd --reload

#需要一个系统用户vmail作为收发邮件的账户,也就是说所有的邮件都是这个vmail用户的

groupadd -g 5000 vmail
useradd -g 5000 -u 5000 vmail -s /sbin/nologin

#vim vmail_mailbox #定义每一个虚拟用户邮件的存放位置,如下省去了根/home/vmail/,原因是主配置文件virtual_mailbox_base = /home/vmail 参数。注意后面"/"
echo [email protected] [email protected]/Maildir/>/etc/postfix/vmail_mailbox
echo [email protected] [email protected]/Maildir/>>/etc/postfix/vmail_mailbox

#创建用户密码表
vim /etc/dovecot/passwd-users-auth
[email protected]:{plain}AAaa1234
[email protected]:{plain}AAaa1234

#适用postconf修改配置 /etc/postfix/main.cf
postconf -e "myhostname = mail.fab.eu.org"
postconf -e "mydomain = fab.eu.org"
postconf -e "myorigin = \$mydomain"
postconf -e "mydestination = localhost,\$mydomain"
postconf -e "mynetworks = 127.0.0.0/8"
postconf -e "inet_interfaces = all"
postconf -e "inet_protocols = ipv4"
postconf -e "smtp_address_preference = ipv4"
postconf -e "home_mailbox = Maildir/"
postconf -e "smtpd_banner = $myhostname ESMTP"
postconf -e "smtpd_sasl_type = dovecot"
postconf -e "smtpd_sasl_auth_enable = yes"
postconf -e "broken_sasl_auth_clients = yes"
postconf -e "smtpd_sasl_path = private/auth"
postconf -e "virtual_mailbox_base = /home/vmail"
postconf -e "virtual_mailbox_maps = hash:/etc/postfix/vmail_mailbox"
postconf -e "virtual_transport = virtual"
postconf -e "virtual_uid_maps = static:5000"
postconf -e "virtual_gid_maps = static:5000"

#本地域之间发件也需要登录验证
postconf -e "smtpd_sender_restrictions =check_sender_access hash:/etc/postfix/sender,reject_unauth_destination"
#邮件大小限制为20M,邮箱大小限制为5G。
postconf -e "message_size_limit = 20485760"
postconf -e "mailbox_size_limit = 5097152000"
postconf -e "show_user_unknown_table_name = no"

#队列超时限制为1天。
postconf -e "bounce_queue_lifetime = 1d"
postconf -e "maximal_queue_lifetime = 1d"

echo fab.eu.org permit_sasl_authenticated, reject>/etc/postfix/sender
postmap /etc/postfix/sender /etc/postfix/vmail_mailbox

#修改保存后检查配置文件是否有错
postfix check

cat /etc/postfix/main.cf | grep -v ^# | grep -v ^$
#启动postfix服务并添加开机自启
systemctl start postfix && systemctl enable postfix

###配置dovecot####
vim /etc/dovecot/dovecot.conf
#修改以下配置

log_path = /var/log/dovecot.log
info_log_path = /var/log/dovecot-info.log
auth_verbose = yes
passdb {
driver = passwd-file
args = /etc/dovecot/passwd-users-auth
}
userdb {
driver = static
args = uid=vmail gid=vmail home=/home/vmail/%u
}

#修改/etc/dovecot/conf.d/10-master.conf文件
vim /etc/dovecot/conf.d/10-master.conf
unix_listener /var/spool/postfix/private/auth {
mode = 0666
}

#修改其他配置文件
sed -i 's|^#protocols = imap pop3 lmtp submission|protocols = imap pop3 lmtp submission|g' /etc/dovecot/dovecot.conf
sed -i 's|^#listen = \*, \:\:|listen = \*, \:\:|g' /etc/dovecot/dovecot.conf
sed -i 's|^ssl = required|ssl = no|g' /etc/dovecot/conf.d/10-ssl.conf
sed -i 's|^#disable_plaintext_auth = yes|disable_plaintext_auth = no|g' /etc/dovecot/conf.d/10-auth.conf
sed -i 's|^auth_mechanisms = plain|auth_mechanisms = plain login|g' /etc/dovecot/conf.d/10-auth.conf
sed -i 's|^#mail_location =|mail_location = maildir:~/Maildir|g' /etc/dovecot/conf.d/10-mail.conf

#查看修改结果
grep -Ev "^$|#" /etc/dovecot/conf.d/10-auth.conf | grep "disable_plaintext_auth \|auth_mechanisms ="
grep -Ev "^$|#" /etc/dovecot/conf.d/10-ssl.conf | grep "ssl ="
grep -Ev "^$|#" /etc/dovecot/conf.d/10-mail.conf | grep mail_location

#启动dovecot服务,并设置成开机自启动
systemctl restart dovecot && systemctl enable dovecot

#用户测试
echo AAaa1234 | doveadm auth test [email protected]
echo AAaa1234 | doveadm auth test [email protected]

#端口查看
ss -lnpt | grep "master\|dovecot"

#foxmail中配置时,用户名是user01 不带@后面的。每次用FOXMAIL测试时注意关闭重开,否则会提示密码错
#有需要重启邮件服务的话,执行下面命令
systemctl restart postfix && systemctl restart dovecot

#----------------------------
#到域名服务商那,添加MX记录,A记录,TXT记录
Type Name TXT Value TTL

A mail 61.188.142.177 #自己云主机的IP
MX @ mail.fab.eu.org #指向自己的A记录mail
TXT @ v=spf1 ip4:61.188.142.177 ~all 600 #添加SPF记录,防止被当垃圾信

#-----------------------------
#使用其他邮箱代收设置方法
#163.com邮箱为例
#设置-账号与邮箱中心,邮箱中心通过POP3自动收取,管理其他邮箱的邮件,立即创建,写邮箱账号,密码,高级设置里接收服务器写你公网邮件域名,如mail.fab.eu.org,端口110

好文啊,最近刚好在搭:xhj06:

用起来咋样?有没有docker镜像?

@“alay”#p128102 就是最简单的一个邮件系统,没有WEB界面,仅只是客户端的收发或通过QQ网易邮箱代收。主打一个简单和轻量,没容器镜像。