Docker自建Email伺服器-已購域名上私人郵件系統

CarlZeng發表於2024-03-12
Docker自建Email伺服器-已購域名上私人郵件系統

搭建Email服務,該方法適用於個人,公司或組織

備註:

這是我找到唯一簡便安裝且能正常使用的郵件系統,請留言告訴我你是否有更好的自建解決方案。

踩了很多的坑,經歷了一個月左右的時間,才測試收發郵件成功(部分郵箱目的地無法達到,由於網際網路上的反垃圾郵件策略),持續更新完善中.

怎麼用

  • 日常收發郵件

    • 透過搭建郵件服務來收發你自己域名下的郵件
  • 第三方平臺(或自己搭建的服務)上需要設定的SMTP服務(SMTP郵箱設定)郵件通知/告知

相關內容

實現方法

iRedMail Docker搭建

iRedMail docker-compose.yml

version: '3.9'
services:
        mariadb:
            image: 'iredmail/mariadb:stable'
            volumes:
                - './postfix_queue:/var/spool/postfix'
                - './sa_rules:/var/lib/spamassassin'
                - './clamav:/var/lib/clamav'
                - './mysql:/var/lib/mysql'
                - './ssl:/opt/iredmail/ssl'
                - './custom:/opt/iredmail/custom'
                - './imapsieve_copy:/var/vmail/imapsieve_copy'
                - './mlmmj-archive:/var/vmail/mlmmj-archive'
                - './mlmmj:/var/vmail/mlmmj'
                - './mailboxes:/var/vmail/vmail1'
                - './backup-mysql:/var/vmail/backup/mysql'
            ports:
                - '587:587'
                - '465:465'
                - '25:25'
                - '993:993'
                - '143:143'
                - '995:995'
                - '110:110'
                - '4433:443'
                - '8093:80'
            env_file:
                - ./iredmail-docker.conf
            container_name: iRedMail

Docker啟動後,假如一切正常則除錯資訊類似於:

docker啟動iRedMail成功後輸出資訊

一切啟動正常後,可以開始設定DNS記錄,然後埠對映出防火牆。

最後可用系統自帶的WEB網頁客戶端來收發郵件,或使用Outlook等客戶端配置SMTP,POP3來收發郵件

設定DNS

Your DNS MX record should point to this value

設定DNS項範例:

image-20240229144603695

image-20240229144758944

| DNS型別 | DNS記錄名 | 內容/值 | 說明/備註 |

DNS型別 DNS記錄名 內容/值 備註/說明
MX carlzeng.top. mail.carlzeng.top. 標識郵件伺服器名稱,DNS MX entry
A mail.carlzeng.top. *.8.18.* 郵件伺服器的IP地址
TXT carlzeng.top. v=spf1 mx -all SPF entry/記錄
TXT dkim._domainkey.carlzeng.top. v=DKIM1; k=rsm; p=* DKIM entry/記錄,含DKIM公鑰public key,詳見下方如何在docker中用命令獲取這個內容值
TXT dmarc.carlzeng.top v=DMARC1; p= reject; rua=* DMARC entry/記錄

問:三個TXT記錄不知道怎麼從iRedMail郵件系統中獲取?

答:After installation: Setup DNS records for your iRedMail server (A, PTR, MX, SPF, DKIM, DMARC)

假如想要檢測DNS記錄的正確性,可以使用:https://mxtoolbox.com/

SPF entry/記錄

v=spf1 mx a:mail.carlzeng.top -all

DKIM entry/記錄

關於如何獲取dkim._domainkey.mydomain.com.的內容/值,舉例:


> docker exec -it iRedMail bash

root@cc9dd27b3e25:/etc/amavis/conf.d# amavisd-new showkeys
; key#1 1024 bits, i=dkim, d=carlzeng.top, /opt/iredmail/custom/amavisd/dkim/carlzeng.top.pem
dkim._domainkey.carlzeng.top.   3600 TXT (
  "v=DKIM1; p="
  "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDUF5BslOb2fARJjXK41xsAPSg"
  "hToQAkJzRuxp5pwaCyqPzIbFNxTZ66z9yw+rbeXYKdpu3bKemHhKVQ7rvnmVlFFL"
  "Nvef7Pk9ddT/nur2T1sfUY6yDu5QRcZArClAQRjfNCFRA11VgsD5q6OKS5GTNtE5"
  "dz3kJGpVdCllilo4OwIDAQAB")

DMARC entry/記錄

v=DMARC1; p=reject; sp=none; adkim=s; aspf=s; rua=mailto:postmaster@carlzeng.top; ruf=mailto:postmaster@carlzeng.top

埠對映

這個步驟的作用是讓路由器上接收到的郵件相關的資料,都轉發給正確的郵件服務系統。

外部埠 內部NAS/Mail主機埠 說明
'587:587'
'465:465'
'25:25'
'993:993'
'143:143'
'995:995'
'110:110'
4433 4433 新增到443埠的訪問可能

將這些埠一一對應,很慶幸這些埠還沒有被ISP遮蔽;

image-20240301105204159

SMTP設定發郵件

比如在佰閱髮卡kamifaka中設定郵箱資訊,用於訊息通知之郵箱通知。

錯誤:無法成功到達郵件目的地

錯誤及解決方法

Docker啟動錯誤:"Permission denied"

iRedMail | /usr/sbin/mysqld: Can't create file '/var/lib/mysql/mysqld.err' (errno: 13 "Permission denied")
iRedMail | 2024-02-28 16:12:07 0 [ERROR] mysqld: Can't create/write to file '/var/lib/mysql/aria_log_control' (Errcode: 13 "Permission denied")

解決辦法:

chmod 777 -R /volume2/KingchuxingSSD512G/docker/compose/iRedMail
chmod 777 -R /volume2/KingchuxingSSD512G/docker/compose/iRedMail/mysql/

DNS設定錯誤之No DMARC Record found

錯誤列表檢測自:https://mxtoolbox.com/emailhealth/carlzeng.top/

Category Host Result
Status Problem dmarc carlzeng.top No DMARC Record found information More Info
Status Problem blacklist mail.carlzeng.top Blacklisted by UCEPROTECTL3 information More Info
Status Problem mx carlzeng.top No DMARC Record found information More Info

錯誤解決:

原來DNS記錄的名稱搞錯了,正確的dmarc DNS記錄名必須是(含下劃線):_dmarc

iredadmin操作不攜帶埠錯誤

操作的後臺https://iredmail.carlzeng.top:3/iredadmin,當提交新的確認操作時,頁面跳轉到未攜帶埠狀態

錯誤解決:

image-20240301101140381

沒有解決自動攜帶埠的問題,等待docker重啟後繼續測試

icloud通訊受阻rejected due to listing in Spamhaus PBL

host mx01.mail.icloud.com[17.56.9.31] said: 550
5.7.1 Mail from IP 111.197.216.113 was rejected due to listing in Spamhaus
PBL. For details please see
http://www.spamhaus.org/query/bl?ip=111.197.216.113 (in reply to RCPT TO
command)

解決辦法:

去給定的IP AND DOMAIN REPUTATION CHECKER網站上提交解封申請,提交成功後,如圖:

image-20240301104242813

程式發郵件被自我攔截為SPAM

從網站平臺配置的SMTP,程式發郵件被自我攔截為SPAM;

Spam scanner report:
 Spam detection software, running on the system "cc9dd27b3e25",
 has identified this incoming email as possible spam.  The original
 message has been attached to this so you can view it or label
 similar future email.  If you have any questions, see
 the administrator of that system for details.

 Content preview:  Test send email

解決辦法: 未知,如何關閉自己對自己發郵件的過度SPAM檢測(郵件不是SPAM,系統誤判)。。。

- https://docs.iredmail.org/disable.spam. … mails.html
- https://docs.iredmail.org/completely.di … assin.html

docker exec -it iRedMail bash

沒有找到這個檔案:/etc/amavis/conf.d/50-user

# @bypass_virus_checks_maps = (1);  # controls running of anti-virus code
# @bypass_spam_checks_maps  = (1);  # controls running of anti-spam code

Restarting Amavisd service is required after changing settings.

> service  amavis restart                                                     
Stopping amavisd: amavisd-new.                                                                                    
Starting amavisd: amavisd-new.              

environment:

TZ=Asia/Shanghai

發現:使用最初的賬戶沒有這個誤判的情況。

是否可刪除mail.**的DNS記錄?

由於設定了泛域名解析道正確的IP ,目前增加的這條mail.carlzeng.top反而增加了DDNS需要去輪詢更新IP的任務數,沒有這條DNS解析,直接ping mail.* 也一樣得到最新且正確的IP地址。

待實踐核實/測試... (理論上沒問題,因為iodine已經成功刪除/最佳化掉A記錄)


以下為踩坑記錄

docker-mailserver

docker-compose.yml (docker-mailserver)

services:
  mailserver:
    image: ghcr.io/docker-mailserver/docker-mailserver:latest
    container_name: mailserver
    # Provide the FQDN of your mail server here (Your DNS MX record should point to this value)
    hostname: mail.carlzeng.top
    ports:
      - "25:25"
      - "465:465"
      - "587:587"
      - "993:993"
    volumes:
      - ./mail-data/:/var/mail/
      - ./mail-state/:/var/mail-state/
      - ./mail-logs/:/var/log/mail/
      - ./config/:/tmp/docker-mailserver/
    environment:
      - ENABLE_RSPAMD=0
      - ENABLE_CLAMAV=0
      - ENABLE_FAIL2BAN=1
      - TZ=Asia/Shanghai
    cap_add:
      - NET_ADMIN # For Fail2Ban to work
    restart: always

docker-compose up 測試中...

下一步要生成配置檔案?

docker exec -ti mailserver setup

mailserver    | [ WARNING ]  You need at least one mail account to start Dovecot (120s left for account creation before shutdown)

docker exec -ti mailserver setup email add service@carlzeng.top

解決辦法:未知

文件資訊:

https://github.com/docker-mailserver/docker-mailserver?tab=readme-ov-file

https://docker-mailserver.github.io/docker-mailserver/latest/

postfix and postfixadmin

docker-compose for postfix and postfixadmin

postfixadmin
Postfix Admin is a web based interface to configure and manage a Postfix based email server for many users.

version: '3'

services:
  db:
    image: mysql:8.0
    restart: always
    environment:
      MYSQL_RANDOM_ROOT_PASSWORD: 1
      MYSQL_DATABASE: postfixadmin
      MYSQL_USER: postfixadmin
      MYSQL_PASSWORD: example

  postfixadmin:
    depends_on:
      - db
    image: postfixadmin
    ports:
      - 8000:80
    restart: always
    environment:
      POSTFIXADMIN_DB_TYPE: mysqli
      POSTFIXADMIN_DB_HOST: db
      POSTFIXADMIN_DB_USER: postfixadmin
      POSTFIXADMIN_DB_NAME: postfixadmin
      POSTFIXADMIN_DB_PASSWORD: example

Where to file issues:
https://github.com/postfixadmin/docker/issues

還差一個靠譜可用的postfix

https://gitlab.com/tozd/docker/postfix

https://gitlab.com/tozd/docker/mail

https://hub.docker.com/r/tozd/postfix

Ports

  • 25/tcp: SMTP port.
  • 465/tcp: SMTPS port.
  • 587/tcp: Mail submission port.

alpine-316`: Postfix 3.7.6

https://hub.docker.com/search?q=postfix

另外一個是:https://github.com/catatnight/docker-postfix

Note:這是我找到唯一簡便安裝且能正常使用的郵件系統,請留言告訴我你是否有更好的自建解決方案。

感謝列表

開源大世界-自託管-電子郵件

Running iRedMail E-Mail Server in Docker

Setup DNS records for your iRedMail server (A, PTR, MX, SPF, DKIM, DMARC)

From zero to full mail server in 20 minutes with Mailu Docker images!

相關文章