本文介绍如何在用Postfix 和Dovecot 在CentOS 6 上搭建一个 POP3/IMAP/SMTP 邮件服务器。你可能不知道啥子是Postfix,这货是Sendmail的完美替代品,sendmail不但发信慢,而且还容易堆积一堆邮件,消耗服务器资源。Postfix不但速度快,而且容易管理,还更安全。
如果你不会使用VI命令,请手动上传下载更新相应文件。路径为VI命令中的路径。
升级系统
screen -U -S mailserver-screen yum update
创建用户组
groupadd vmail -g 2222
创建用户
useradd vmail -r -g 2222 -u 2222 -d /var/vmail -m -c "mail user"
安装Postfix
yum remove exim sendmail yum install postfix cronie
修改postfix 配置文件 main.cf
cp /etc/postfix/main.cf{,.orig} vim /etc/postfix/main.cf
myhostname = fatesinger.com mydomain = fatesinger.com myorigin = $mydomain queue_directory = /var/spool/postfix command_directory = /usr/sbin daemon_directory = /usr/libexec/postfix data_directory = /var/lib/postfix mail_owner = postfix unknown_local_recipient_reject_code = 550 alias_maps = hash:/etc/postfix/aliases alias_database = $alias_maps inet_interfaces = all inet_protocols = ipv4 mydestination = $myhostname, localhost.$mydomain, localhost debug_peer_level = 2 debugger_command = PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin ddd $daemon_directory/$process_name $process_id & sleep 5 sendmail_path = /usr/sbin/sendmail.postfix newaliases_path = /usr/bin/newaliases.postfix mailq_path = /usr/bin/mailq.postfix setgid_group = postdrop html_directory = no manpage_directory = /usr/share/man sample_directory = /usr/share/doc/postfix-2.6.6/samples readme_directory = /usr/share/doc/postfix-2.6.6/README_FILES relay_domains = * virtual_alias_maps=hash:/etc/postfix/vmail_aliases virtual_mailbox_domains=hash:/etc/postfix/vmail_domains virtual_mailbox_maps=hash:/etc/postfix/vmail_mailbox virtual_mailbox_base = /var/vmail virtual_minimum_uid = 2222 virtual_transport = virtual virtual_uid_maps = static:2222 virtual_gid_maps = static:2222 smtpd_sasl_auth_enable = yes smtpd_sasl_type = dovecot smtpd_sasl_path = /var/run/dovecot/auth-client smtpd_sasl_security_options = noanonymous smtpd_sasl_tls_security_options = $smtpd_sasl_security_options smtpd_sasl_local_domain = $mydomain broken_sasl_auth_clients = yes smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
创建vmail_domains
配置文件
vim /etc/postfix/vmail_domains
mydomain.com OK my-otherdomain.com OK
创建vmail_mailbox
配置文件
vim /etc/postfix/vmail_mailbox
info@mydomain.com mydomain.com/info/ info@my-otherdomain.com my-otherdomain.com/info/
创建vmail_aliases
配置文件
vim /etc/postfix/vmail_aliases
info@mydomain.com info@mydomain.com info@my-otherdomain.com foo@bar.tld
hash 配置文件
postmap /etc/postfix/vmail_domains postmap /etc/postfix/vmail_mailbox postmap /etc/postfix/vmail_aliases
touch /etc/postfix/aliases vim +/submission /etc/postfix/master.cf
submission inet n - n - - smtpd
安装DOVECOT
yum install dovecot
修改dovecot的配置文件dovecot.conf
cp /etc/dovecot/dovecot.conf{,.orig} vim /etc/dovecot/dovecot.conf
listen = * ssl = no protocols = imap lmtp disable_plaintext_auth = no auth_mechanisms = plain login mail_access_groups = vmail default_login_user = vmail first_valid_uid = 2222 first_valid_gid = 2222 #mail_location = maildir:~/Maildir mail_location = maildir:/var/vmail/%d/%n passdb { driver = passwd-file args = scheme=SHA1 /etc/dovecot/passwd } userdb { driver = static args = uid=2222 gid=2222 home=/var/vmail/%d/%n allow_all_users=yes } service auth { unix_listener auth-client { group = postfix mode = 0660 user = postfix } user = root } service imap-login { process_min_avail = 1 user = vmail }
创建虚拟用户的密码文件。用户名和密码会储存在这里。
touch /etc/dovecot/passwd doveadm pw -s sha1 | cut -d '}' -f2
vim /etc/dovecot/passwd
info@mydomain.com:DOzcsKI8HY0bg8LAuz0DPKwS3WA=
chown root: /etc/dovecot/passwd chmod 600 /etc/dovecot/passwd
启动服务
chkconfig postfix on chkconfig dovecot on service postfix restart service dovecot restart
如果是centos7 则使用如下命令
systemctl enable postfix.service systemctl enable dovecot.service systemctl start postfix.service systemctl start dovecot.service
之后就可以测试收发邮件了。
测试邮件总是密码错误。。
535 5.7.8 Error: authentication failed: UGFzc3dvcmQ6
尝试修改了几次密码
@lock telnet测试发送已经显示
250 2.0.0 Ok: queued as 86AAF1903E5
但是就是收不到邮件,wordpress smtp插件也不能发送