Skip to content

Instantly share code, notes, and snippets.

@mavaddat
Forked from yano3nora/docker_mail_servers.md
Last active January 24, 2022 19:39
Show Gist options
  • Save mavaddat/27c1cc93d5b38ef26c53558bb8f43dce to your computer and use it in GitHub Desktop.
Save mavaddat/27c1cc93d5b38ef26c53558bb8f43dce to your computer and use it in GitHub Desktop.
[docker: Mail server container] Mail servers by Docker. #docker

About Mail Hog

It is a kind of simple SMTP server and can be used for mail test during development. Because the container is also published on Docker Hub It's also very easy to deploy.

This time, we will just add an SMTP server to the existing docker project. If you want to check the behavior with docker run, please check the document of the distributor.

Installation method

Add a container to docker-compose.yml

mailhog:
  image: mailhog/mailhog
  ports:
    - "8025:8025"

The 8025 set to ports is MailHog's default HTTP port. If you want to do an SMTP test from the host terminal to MailHog on the container Add -" 1025: 1025 " to ports

service default port
HTTP server 8025
SMTP server 1025

After building, when you access http://localhost:8025, this screen will be displayed. 2d0659dc-aa77-7b14-9804-8d3380759a49.png

After that, if you send an email to the MailHog container, you can check it on this screen

Email test

For the same accuracy, check with a simple program whether SMTP communication passes normally.

import smtplib
from email.mime.text import MIMEText

def test_mail(fromaddr, toaddr, subject, msg):
    m = MIMEText(msg)
    m['Subject'] = subject
    m['From'] = fromaddr
    m['To'] = toaddr

    s=smtplib.SMTP(host="mailhog", port=1025)
    s.sendmail(fromaddr, toaddr, m.as_string())
    s.close()

    return "done"

Incoming mail is displayed in a list 22c03870-9b77-0ff5-5499-db36277354d4.png

You can also check the source and header information from the browser. f5455a56-3179-3e58-989a-2ff6852e4bb2.png

failure_test in Jim

It seems that Jim can also test the abnormal system. To use it, add the following in the mailhog part of docker-compose.yml

command: 
  -invite-jim 
  -jim-accept = 0.5

In the above case, there is a 50% chance that the email will be sent successfully. (Other communication speeds can be set) In the manual, it says Mail Hog ~ as an option setting, but if you provide that on Docker, it will not work

Please check various options from the following Introduction to Jim

You can also check the setting value from the browser. 18dcb8e8-c234-6df8-ad0c-71387607ca45.png

Additional info

By default, emails are stored in memory, so If you restart the container, the received mail will be deleted.


Reference article

Qiita: Test sending and receiving emails with MailHog

MAILHOG

mailhog/MailHog - github.com
mailhog/mailhog - hub.docker.com
MailHog for sending emails from a Docker container test environment

A Docker container mail server with a relatively large number of stars. It's published on Docker Hub, so you can quickly use this to test sending of emails by SMTP in a Docker environment.

# docker-compose.yml
services: 
  mailhog:
    image: mailhog/mailhog
    ports:
      - "8025:8025"  # Connect from GUI to test http://localhost:8025
      - "1025:1025"  # Specify this for the outbound SMTP port

DOCKER-MAILSERVER

tomav/docker-mailserver - github.com
tvial/docker-mailserver - hub.docker.com
Build a server with Docker Compose, SMTP + POP3 server edition

This is an apt mail server container. It is supposed to be used in a production environment and supports fail2ban, antivirus / spam, and TLS. You can do anything with just the config file! Easy peezy.

services:
  mail:
    image: tvial/docker-mailserver:stable
    container_name: mail-server
    domainname: example.com
    ports:
      - "25:25"
      - "110:110"
    volumes:
      - ./.docker/mail/config/:/tmp/docker-mailserver/
    environment:
      # Uncomment the following line if you want to debug
      # - DMS_DEBUG=1
      - ENABLE_SPAMASSASSIN=0
      - ENABLE_CLAMAV=0
      - ENABLE_FETCHMAIL=0
      - ENABLE_FAIL2BAN=0
      - ENABLE_POSTGREY=0
      - ENABLE_POP3=1
    cap_add:
      - NET_ADMIN
      - SYS_PTRACE
    restart: always
# .docker/mail/config/postfix-accounts.cf
user@example.com|{PLAIN}your-password-here

# .docker/mail/config/postfix-main.cf
mydestination =
smtpd_recipient_restrictions =

# .docker/mail/config/dovecot.cf
disable_plaintext_auth = no
ssl = no
# Uncomment the following line if you want to debug
# auth_verbose = yes
# auth_debug = yes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment