最新ubuntu搭建公网个人邮件服务器(基于postfix,dovecot,mysql) (2)

  创建/etc/postfix/mysql-virtual-mailbox-domains.cf,内容如下:

user = admin password = 123456 port = 3306 hosts = 127.0.0.1 dbname = mailserver query = SELECT 1 FROM virtual_domains WHERE

接着重启 postfix,并测试 postfix 能否找到域,如果成功返回 1:

service postfix restart postmap -q test.com mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf

  创建/etc/postfix/mysql-virtual-mailbox-maps.cf,内容如下:

user = admin password = 123456 port = 3306 hosts = 127.0.0.1 dbname = mailserver query = SELECT 1 FROM virtual_users WHERE email='%s'

接着重启 postfix,并测试其能否找到邮箱地址,成功返回 1:

service postfix restart postmap -q first@test.com mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf

  最后创建/etc/postfix/mysql-virtual-alias-maps.cf,内容如下:

user = admin password = 123456 port = 3306 hosts = 127.0.0.1 dbname = mailserver query = SELECT destination FROM virtual_aliases WHERE source='%s'

同样重启 postfix,验证能否正确找到别名,并返回:

service postfix restart postmap -q first@test.com mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf

  如果响应使用 587 端口来进行俺的 smtp 通信,需修改/etc/postfix/master.cf 文件:
取消以下行的注释:

最新ubuntu搭建公网个人邮件服务器(基于postfix,dovecot,mysql)

配置 dovecot

  postfix 配置完毕,现在来配置 dovecot,首先编辑主配置文件/etc/dovecot/dovecot.conf:

首先确保下面一行是启用的:

!include conf.d/*.conf

然后在配置文件的最后加入如下配置,启用各协议:

protocols = imap lmtp pop3

  修改/etc/dovecot/conf.d/10-mail.conf,确保存在以下两个配置:

mail_location = maildir:/var/mail/vhosts/%d/%n mail_privileged_group = mail

上面的配置将邮件存放目录设置在/var/mail 中,因此将该文件夹的所属人改为 vmail/vmail.命令如下:

groupadd -g 5000 vmail useradd -g vmail -u 5000 vmail -d /var/mail chown -R vmail:vmail /var/mail

  修改/etc/dovecot/conf.d/10-auth.conf。首先确保如下两个配置存在且值正确:

disable_plaintext_auth = yes auth_mechanisms = plain login

然后修改配置以禁用系统用户登陆,并开启 mysql 支持,如下图所示:

启用mysql支持

  修改/etc/dovecot/dovecot-sql.conf.ext文件,将内容改成下面的内容:

passdb { driver = sql args = /etc/dovecot/dovecot-sql.conf.ext } userdb { driver = static args = uid=vmail gid=vmail home=http://www.likecs.com/var/mail/vhosts/%d/%n }

  修改/etc/dovecot/dovecot-sql.conf.ext:
首选取消 driver 参数注释并设置为 mysql

driver = mysql

然后取消 connect 行注释并设置为如下内容:

connect = host=127.0.0.1 port=3306 dbname=mailserver user=admin password=123456

接着取消 default_pass_scheme 行的注释并改为 MD5

default_pass_scheme = MD5

接着取消 password_query 行的注释并设置为以下信息:

password_query = SELECT email as user, password FROM virtual_users WHERE email='%u';

最后将/etc/dovecot的拥有者改为 vmail:dovecot

chown -R vmail:dovecot /etc/dovecot chmod -R o-rwx /etc/dovecot

  修改/etc/dovecot/conf.d/10-master.conf:

首先将 imap-login , pop3-login 下第一个的 port 设置为 0,以禁用非 ssl 加密的 imap 和 pop3 协议,如下图所示:

最新ubuntu搭建公网个人邮件服务器(基于postfix,dovecot,mysql)

然后找到service lmtp将其修改为如下:

service lmtp { unix_listener /var/spool/postfix/private/dovecot-lmtp { mode = 0600 user = postfix group = postfix } # Create inet listener only if you can't use the above UNIX socket #inet_listener lmtp { # Avoid making LMTP visible for the entire internet #address = #port = #} }

然后找到service auth将其内容修改为如下:

service auth { unix_listener /var/spool/postfix/private/auth { mode = 0666 user = postfix group = postfix } unix_listener auth-userdb { mode = 0600 user = vmail #group = } user = dovecot }

最后找到service auth-worker改为如下内容:

service auth-worker { # Auth worker process is run as root by default, so that it can access # /etc/shadow. If this isn't necessary, the user should be changed to # $default_internal_user. user = vmail }

  最后要改的就是/etc/dovecot/conf.d/10-ssl.conf,以开启 ssl 认证.

首先将 ssl 参数改为 required:

ssl = required

然后设置 ssl 证书路径就 ok 了,还是用之前的 ssl 证书:

ssl_cert = </etc/letsencrypt/live/test.com/fullchain.pem ssl_key = </etc/letsencrypt/live/test.com/privkey.pem

  到这里所有的配置都 OK,重启 postfix,dovecot 后就可以用邮箱客户端(比如 foxmail)连接了。

service postfix restart service dovecot restart 结束

  配合一个邮件客户端看似很简单,实际上还是有很多坑的,看看上面那么多的配置项就知道了,一定要耐心。

  如果无法登陆,可以看看 postfix 和 dovecot 的日志报错情况,再去修改。日志位置在/var/log

注意:
被这个问题困扰了好几天,未找到解决办法,最后放弃.

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpdzpf.html