容器(Docker)自建邮箱poste.io

2021-07-04
2分钟阅读时长

使用poste.io(Docker)自建邮局

在若大的互联网,我们都会通过邮箱注册各种各样的账号,但是很多时候只有一个主邮箱有着诸多不便,所以我们希望,最好是一个平台对应一个email,且无限制。

我在找了很多可以私有部署的邮局之后,最终选择了poste.io,只看中了一个点,轻量化部署,1G内存完全足矣,而且部署方便,也就意味着迁移方便。

环境

下面我将在腾讯云的VPS上面部署,配置如下:

1 Core 1G RAM Ubuntu +宝塔

uname -a
Linux VM-*-*-ubuntu 5.4.110-bbrplus #1 SMP Sat Apr 10 01:14:42 CST 2021 x86_64 x86_64 x86_64 GNU/Linux

docker和docker-componse安装请参考Install Docker Engine on CentOSInstall Docker Compose

运行poste.io

  • 创建poste.io运行目录
$ mkdir -p /www/wwwroot/tools/mail
$  cd /www/wwwroot/tools/mail
  • 创建数据存放目录
$ mkdir data
  • 创建docker-compose.yml配置文件
$ nano docker-compose.yml
version: "3.3"

services:
  mail:
    image: analogic/poste.io
    restart: always
    hostname: mail.kquark.com
    ports:
     - "25:25"
     - "172.17.0.1:62030:8080"
     #- "4433:4433"
     - "110:110"
     - "143:143"
     - "465:465"
     - "587:587"
     - "993:993"
     - "995:995"
    environment:
     - TZ=Asia/Shanghai
     - DISABLE_CLAMAV=TRUE
     - DISABLE_RSPAMD=TRUE
     - HTTPS=OFF
     - HTTP_PORT=8080
     #- HTTPS_PORT=4433
    volumes:
     - ./data:/data

由于https是在NGINX上面配置的,所以poste.io只运行80端口且绑定在172.17.0.1:8080这个docker的host地址。

下面两个配置是关闭反垃圾邮件的功能,实测可以节省很多内存

- DISABLE_CLAMAV=TRUE
- DISABLE_RSPAMD=TRUE

在部署中,HTTPS我们在NGINX中上面配置,为了可以共用80和443,至于那些选项是什么意思,可以参考Getting started,官方文档会有说明。

  • 运行
$ docker-compose up -d
  • 出错
Status: Downloaded newer image for analogic/poste.io:latest
Creating mail_mail_1 ...
Creating mail_mail_1 ... error

ERROR: for mail_mail_1  Cannot start service mail: driver failed programming external connectivity on endpoint mail_mail_1 (999b3565861677c81593522d582fc590e50576437d6551e70db650a4ed73fc86): Error starting userland proxy: listen tcp4 0.0.0.0:587: bind: address already in use

ERROR: for mail  Cannot start service mail: driver failed programming external connectivity on endpoint mail_mail_1 (999b3565861677c81593522d582fc590e50576437d6551e70db650a4ed73fc86): Error starting userland proxy: listen tcp4 0.0.0.0:587: bind: address already in use
ERROR: Encountered errors while bringing up the project.

root@VM-*-*-ubuntu:/www/wwwroot/tools/mail# lsof -i:587
COMMAND     PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
master  3167292 root   18u  IPv4 49042101      0t0  TCP *:submission (LISTEN)
master  3167292 root   19u  IPv6 49042102      0t0  TCP *:submission (LISTEN)
root@VM-*-*-ubuntu:/www/wwwroot/tools/mail# kill -9 3167292
root@VM-*-*-ubuntu:/www/wwwroot/tools/mail# lsof -i:587
  • 查看日志
$ docker-compose logs -f
# 当出现下面的提示表示启动完成
......

mail_1  | [cont-init.d] 98-timezone.sh: exited 0.
mail_1  | [cont-init.d] 99-custom-plugins: executing...
mail_1  | [cont-init.d] 99-custom-plugins: exited 0.
mail_1  | [cont-init.d] done.
mail_1  | [services.d] starting services
mail_1  |
mail_1  |
mail_1  |  Poste.io administration available at https://172.19.0.2:443 or http://172.19.0.2:8080
mail_1  |
mail_1  |
mail_1  | [services.d] done.

配置DNS解析

官方文档请参考Configuring DNS,我的域名是放在Cloudflare的,首先我们需要加一条A记录,如下:

增加imappopsmtp记录

增加MXSPFDMARC

其实我都已经加好了,所以我只是截图把配置发出来,按照上面的配置对照着来就可以

配置宝塔

做好反代,反代172.17.0.1:62030,略

浏览器访问https://mail.ansheng.me/,会让你配置管理员的账号和密码,做对应的设置就可以

4

配置

登陆后台的面板大概长这样

5

  • TLS

选择左侧栏的System settings -> TLS certificate,我们需要上传ssl证书到poste.io,否则客户端TLS无法使用

6

  • DKIM

为了防止进入垃圾箱,我们需要添加DKIM的配置,找到Virtual domains -> mail.ansheng.me请选择自己的域名 -> 点击create new key

8

9

在到DNS里面添加以下记录

10

  • 收发件测试

配置完成之后我们可以退出登录,然后登陆https://mail.***.***/webmail/,发送一封邮件到其他邮箱测试是否可以收到

打开Gmail,很快就会收到邮件,有可能会进垃圾箱,可能解析还没生效

然后我们回复邮件看是否能收到

  • 客户端

如果你需要配置客户端,例如通过Gmail、Foxmail这些第三方的,请参考Example client settings,注意host和端口,我在iOS上面的Gmail已经配置成功,可以收发邮件,正常使用。

结尾

  • 端口说明
端口 描述
25 SMTP – mostly processing incoming mails
110 POP3 – standard protocol for accessing mailbox, STARTTLS is required before client auth
143 IMAP – standard protocol for accessing mailbox, STARTTLS is required before client auth
465 SMTPS – Legacy SMTPs port
587 MSA – SMTP port used primarily for email clients after STARTTLS and auth
993 IMAPS – alternative port for IMAP encrypted since connection
995 POP3S – encrypted POP3 since connections

其实在了解poste.io是怎么运行的原理之后,在研究其他邮局也都是大同小异。

参考:https://ansheng.me/container-docker-deploy-poste-io-private-post-office/