這是本文件的舊版!


Mailpit(開源 SMTP 模擬服務)

  • 因為要驗證應用系統寄信的狀況, 透過實際 Mail Server 雖然也可以驗證, 但效率比較差也常發現收不到信是被中間的一些 SPAM 的機制攔截, 因此有不少開源 SMTP 模擬服務專案出現
  • 評估與使用過 Fake SMTP Server MailHog 後發現都不支援 STARTTLS, 最後問了 AI 才推薦 Mailpit
  • docker-compose.yml
    services:
      mailpit:
        image: axllent/mailpit
        container_name: mailpit
        ports:
          - "1025:1025" # SMTP
          - "8025:8025" # Web
        environment:
          - MP_SMTP_TLS_CERT=/etc/mailpit/ServerCA.crt
          - MP_SMTP_TLS_KEY=/etc/mailpit/ServerCA.key
          - MP_SMTP_AUTH_ACCEPT_ANY=true
          - MP_SMTP_REQUIRE_STARTTLS=true   # STARTTLS
          - MP_SMTP_REQUIRE_TLS=false       # SSL/TLS
          - MP_UI_LANG=zh_TW
          - MP_UI_AUTO_REFRESH=10
        volumes:
          - ./certs:/etc/mailpit
        restart: unless-stopped

    https://raw.githubusercontent.com/tryweb/docker-compose/refs/heads/main/mailpit/docker-compose.yml

  • 參考 OpenSSL 簽發憑證方式 來產生SSL憑證
  • 將產生的 ServerCA.cer 與 ServerCA.key 放入 certs 目錄內
  • 啟動 docker compose 即可提供 SMTP 服務
  • 環境變數 MP_SMTP_REQUIRE_STARTTLS=true 表示設定為 STARTTLS, 測試方式

    openssl s_client -connect localhost:1025 -starttls smtp

  • 環境變數 MP_SMTP_REQUIRE_TLS=true 表示設定為 SSL/TLS, 測試方式

    openssl s_client -connect localhost:1025

  • 正確回應的結果應該可以看到憑證的內容 Exp.

    $ openssl s_client -connect localhost:1025 -starttls smtp
    CONNECTED(00000003)
    Can't use SSL_get_servername
    depth=0 C = TW, ST = Taiwan, L = Taipei, O = III, OU = DevOps, CN = MailHog, emailAddress = [email protected]
    verify error:num=20:unable to get local issuer certificate
    verify return:1
    depth=0 C = TW, ST = Taiwan, L = Taipei, O = III, OU = DevOps, CN = MailHog, emailAddress = [email protected]
    verify error:num=21:unable to verify the first certificate
    verify return:1
    depth=0 C = TW, ST = Taiwan, L = Taipei, O = III, OU = DevOps, CN = MailHog, emailAddress = [email protected]
    verify return:1
    ---
    Certificate chain
     0 s:C = TW, ST = Taiwan, L = Taipei, O = III, OU = DevOps, CN = MailHog, emailAddress = [email protected]
       i:C = TW, ST = Taiwan, L = Taipei, O = III, OU = DevOps, CN = Jonathan Tsai, emailAddress = [email protected]
       a:PKEY: rsaEncryption, 2048 (bit); sigalg: RSA-SHA1
       v:NotBefore: Apr 12 02:31:31 2025 GMT; NotAfter: Apr 12 02:31:31 2027 GMT
    ---
    Server certificate
    -----BEGIN CERTIFICATE-----
    MIIDmjCCAoICFBr9dfIlHO4wEY7hxz/BJNhTuageMA0GCSqGSIb3DQEBBQUAMIGK
    MQswCQYDVQQGEwJUVzEPMA0GA1UECAwGVGFpd2FuMQ8wDQYDVQQHDAZUYWlwZWkx
    :

網頁

  • tech/mailpit.1744475059.txt.gz
  • 上一次變更: 2025/04/13 00:24
  • jonathan