Programster's Blog

Tutorials focusing on Linux, programming, and open-source



MailHog is a free and open source (GitHub) email-testing tool with a fake SMTP server underneath. It is designed for developers, allowing them to test an applications email sending capability without needing to hook up to a real SMTP server. The developers can go into the MailHog web UI to view all of the sent emails. This also means that you can test an application without the risk of "real" emails going out to actual people.

Demonstration Codebase

For this tutorial, I created an example codebase on GitHub to allow you to quickly deploy Mailhog with an emailer application that sends a test email once every three seconds. Below is the docker-compose.yml file for quick reference if you just want to see how the MailHog service is defined, so you can quickly get it configured within your application:

Docker Compose File

version: "3.9"

      context: .
      dockerfile: ./Dockerfile
    container_name: emailer
    image: mailhog-emailer
        - mailhog
      - SMTP_HOST=mailhog
      - SMTP_USER=null
      - SMTP_PASSWORD=null
      - SMTP_PORT=1025
      - SMTP_PROTOCOL=none
      - SMTP_FROM_NAME=myAppName
      - MAIL_TO_NAME=My Name

  # Mailhog fake SMTP server
    container_name: mailhog
    image: mailhog/mailhog
#    Uncomment the logging lines below if you need to disable logging
#    logging:
#      driver: 'none'  # disable saving logs
        - 1025:1025 # smtp server
        - 8025:8025 # web ui

As you can see from the service definition, it is extremely easy to set up and configure with not requiring any environment variables or volumes needing to be set up.

Build and Run

docker-compose build
docker-compose up

Then you can view the generated emails at http://localhost:8025/

Last updated: 27th July 2023
First published: 27th July 2023