【ubuntu mail】ubuntu20.04+postfix+dovecot+postfixadmin+postgres搭建内网邮箱服务器

1. 安装postfixadmin

 https://www.cnblogs.com/fireblackman/p/15799597.html

2. 安装postgres

 https://www.cnblogs.com/fireblackman/p/15740550.html

3. 安装postfix

# mysql用postfix-mysql, postgres使用postfix-pgsql

#sudo apt-get install postfix postfix-mysql
#因为我们用的是postgres, 所以我们使用如下
sudo apt-get install postfix postfix-pgsql

4. 安装dovecot

#sudo apt-get install dovecot-core dovecot-imapd dovecot-pop3d dovecot-lmtpd dovecot-mysql
#同postfix, 我们使用的是postgres, 所以使用如下
sudo apt-get install dovecot-core dovecot-imapd dovecot-pop3d dovecot-lmtpd dovecot-pgsql 

5. 配置postfix

a. 建立三个数据库配置文件

sudo vim /etc/postfix/postgres_virtual_mailbox_maps.cf
sudo vim /etc/postfix/postgres_virtual_domains_maps.cf
sudo vim /etc/postfix/postgres_virtual_alias_maps.cf
 ##/etc/postfix/postgres_virtual_alias_maps.cf
 user = postfixadmin
 password = postfixadmin
 hosts = 127.0.0.1
 dbname = postfixadmin
 table = alias
 select_field = goto
 where_field = address
 
 ##/etc/postfix/postgres_virtual_domains_maps.cf
 user = postfixadmin
 password = postfixadmin
 hosts = 127.0.0.1
 dbname = postfixadmin
 table = domain
 select_field = domain
 where_field = domain
 
 ##/etc/postfix/postgres_virtual_mailbox_maps.cf
 user = postfixadmin
 password = postfixadmin
 hosts = 127.0.0.1
 dbname = postfixadmin
 table = mailbox
 select_field = maildir
 where_field = username

b.配置/etc/postfix/main.cf文件

sudo vim /etc/postfix/main.cf

#smtpd_tls_cert_file = /etc/ssl/certs/cacert.pem
#smtpd_tls_key_file = /etc/ssl/private/server.key

#smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
#smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
#smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
# information on enabling SSL in the smtp client.

smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
#myhostname = test.com
myhostname = mail.test.com
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
#mydestination = $myhostname, mail.test.com, test.com, localhost.localdomain, localhost
mydestination =
relayhost =
#mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mynetworks = all
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = all


# Virtual Mailbox Domain Settings
virtual_alias_maps = pgsql:/etc/postfix/postgres_virtual_alias_maps.cf
virtual_mailbox_domains = pgsql:/etc/postfix/postgres_virtual_domains_maps.cf
virtual_mailbox_maps = pgsql:/etc/postfix/postgres_virtual_mailbox_maps.cf
virtual_mailbox_limit = 51200000
virtual_minimum_uid = 5000
virtual_uid_maps = static:5000
virtual_gid_maps = static:5000
virtual_mailbox_base = /home/vmail
virtual_transport = virtual

#SASL AS Dovecot to authenticate.
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
broken_sasl_auth_clients = yes
smtpd_sasl_security_options = noanonymous
smtpd_sasl_local_domain =
smtpd_sasl_authenticated_header = yes
smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination

c.更改/etc/postfix/master.cf配置

sudo vim /etc/postfix/master.cf
# Interfaces to non-Postfix software. Be sure to examine the manual
# pages of the non-Postfix software to find out what options it wants.
#
# Many of the following services use the Postfix pipe(8) delivery
# agent.  See the pipe(8) man page for information about ${recipient}
# and other message envelope options.
# ====================================================================
#
# maildrop. See the Postfix MAILDROP_README file for details.
# Also specify in main.cf: maildrop_destination_recipient_limit=1
#
maildrop  unix  -       n       n       -       -       pipe
  flags=DRhu user=vmail argv=/usr/bin/maildrop -d ${recipient}
#
# ====================================================================
#
# Recent Cyrus versions can use the existing "lmtp" master.cf entry.
#
# Specify in cyrus.conf:
#   lmtp    cmd="lmtpd -a" listen="localhost:lmtp" proto=tcp4
#
# Specify in main.cf one or more of the following:
#  mailbox_transport = lmtp:inet:localhost
#  virtual_transport = lmtp:inet:localhost
#
# ====================================================================
#
# Cyrus 2.1.5 (Amos Gouaux)
# Also specify in main.cf: cyrus_destination_recipient_limit=1
#
#cyrus     unix  -       n       n       -       -       pipe
#  user=cyrus argv=/cyrus/bin/deliver -e -r ${sender} -m ${extension} ${user}
#
# ====================================================================
# Old example of delivery via Cyrus.
#
#old-cyrus unix  -       n       n       -       -       pipe
#  flags=R user=cyrus argv=/cyrus/bin/deliver -e -m ${extension} ${user}
#
# ====================================================================
#
# See the Postfix UUCP_README file for configuration details.
#
uucp      unix  -       n       n       -       -       pipe
  flags=Fqhu user=uucp argv=uux -r -n -z -a$sender - $nexthop!rmail ($recipient)
#
# Other external delivery methods.
#
ifmail    unix  -       n       n       -       -       pipe
  flags=F user=ftn argv=/usr/lib/ifmail/ifmail -r $nexthop ($recipient)
bsmtp     unix  -       n       n       -       -       pipe
  flags=Fq. user=bsmtp argv=/usr/lib/bsmtp/bsmtp -t$nexthop -f$sender $recipient
scalemail-backend unix  -       n       n       -       2       pipe
  flags=R user=scalemail argv=/usr/lib/scalemail/bin/scalemail-store ${nexthop} ${user} ${extension}
mailman   unix  -       n       n       -       -       pipe
  flags=FR user=list argv=/usr/lib/mailman/bin/postfix-to-mailman.py
  ${nexthop} ${user}

  

6.设置文件权限以及Vmail用户

 cd /etc/postfix
 chown root:postfix postgres_virtual_*
 chmod 640 postgres_virtual_*
 groupadd -g 5000 vmail
 useradd -m -g vmail -u 5000 -d /home/vmail vmail
 service postfix restart 

 7. 配置 dovcot

a. 修改相关配置文件

/etc/dovecot/dovecot.conf 
/etc/dovecot/conf.d/10-master.conf 
/etc/dovecot/conf.d/10-ssl.conf 
/etc/dovecot/conf.d/10-auth.conf
/etc/dovecot/conf.d/10-mail.conf 
/etc/dovecot/dovecot-sql.conf.ext
sudo vim /etc/dovecot/dovecot.conf
!include_try /usr/share/dovecot/protocols.d/*.protocol
protocols = imap pop3 lmtp
sudo vim /etc/dovecot/conf.d/10-master.conf                     //未列出的服务配置保持不动
service auth {
  # auth_socket_path points to this userdb socket by default. It's typically
  # used by dovecot-lda, doveadm, possibly imap process, etc. Users that have
  # full permissions to this socket are able to get a list of all usernames and
  # get the results of everyone's userdb lookups.
  #
  # The default 0666 mode allows anyone to connect to the socket, but the
  # userdb lookups will succeed only if the userdb returns an "uid" field that
  # matches the caller process's UID. Also if caller's uid or gid matches the
  # socket's uid or gid the lookup succeeds. Anything else causes a failure.
  #
  # To give the caller full permissions to lookup all users, set the mode to
  # something else than 0666 and Dovecot lets the kernel enforce the
  # permissions (e.g. 0777 allows everyone full permissions).
  unix_listener auth-userdb {
    mode = 0600
    user = vmail
    #group = 
  }

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

  # Auth process is run as this user.
  #user = $default_internal_user
  user = dovecot
}
sudo vim /etc/dovecot/conf.d/10-ssl.conf
ssl = required
sudo vim /etc/dovecot/conf.d/10-auth.conf
auth_mechanisms = plain login                        //修改此处
#!include auth-system.conf.ext                       //注释掉此处
!include auth-sql.conf.ext                           //取消此行的注释
#!include auth-ldap.conf.ext
#!include auth-passwdfile.conf.ext
#!include auth-checkpassword.conf.ext
#!include auth-vpopmail.conf.ext
#!include auth-static.conf.ext
sudo vim /etc/dovecot/conf.d/10-mail.conf
mail_location = maildir:/home/vmail/%u                //修改此处, 这里关系到邮件的读取,不然会收不到邮件
mail_privileged_group = vmail                         //添加此处
sudo vim /etc/dovecot/dovecot-sql.conf.ext
driver = pgsql
connect = host=127.0.0.1 dbname=postfixadmin user=postfixadmin password=postfixadmin # 这里配置你实际的数据库连接
default_pass_scheme = SHA512-CRYPT

iterate_query = SELECT username AS user FROM mailbox;

user_query = SELECT CONCAT('/home/vmail/',maildir) AS home, 5000 AS uid, 5000 AS gid FROM mailbox WHERE username = '%u' AND active = '1';  # 这里要和mail_location配置对应上,不然读不到邮件
#user_query = SELECT CONCAT('/home/vmail/', username) AS home, 5000 AS uid, 5000 AS gid FROM mailbox WHERE username = '%u' AND active = '1';

password_query = SELECT username AS user,password FROM mailbox WHERE username = '%u' AND active='1';

b.更改配置文件权限并重启dovecot

chown -R vmail:dovecot /etc/dovecot
chmod -R o-rwx /etc/dovecot
sudo service dovecot restart

8. 使用postfixadmin管理邮箱账号

http://{你的ip}/postfixadmin/login.php

 

 

新建域, 添加自己电脑上配置的域名

 

 

 新建邮箱,添加测试邮件,比如jenkins, test等

 

 

9. 使用foxmail测试邮件配置

 

 使用postfixadmin的管理员发送邮件给测试用户

 

 查看接收

 10. 查看定位日志

tail -f /var/log/mail.log

11. 邮件存放目录, 主要查看配置的目录和数据库中配置的目录是否一致,不一致会收不到邮件

cd /home/vmail/  && ls -al

 

 至此,基本完成

参考链接:https://zhuanlan.zhihu.com/p/44886589

 

 

 

 
posted @ 2022-02-16 20:57  代码诠释的世界  阅读(1173)  评论(0编辑  收藏  举报