MailHog
About
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"
services:
app:
build:
context: .
dockerfile: ./Dockerfile
container_name: emailer
image: mailhog-emailer
depends_on:
- mailhog
environment:
- SMTP_HOST=mailhog
- SMTP_USER=null
- SMTP_PASSWORD=null
- SMTP_PORT=1025
- SMTP_PROTOCOL=none
- SMTP_FROM_EMAIL=myapp@gmail.com
- SMTP_FROM_NAME=myAppName
- MAIL_TO_EMAIL=myemail@gmail.com
- MAIL_TO_NAME=My Name
# Mailhog fake SMTP server
# https://github.com/mailhog/MailHog
mailhog:
container_name: mailhog
image: mailhog/mailhog
# Uncomment the logging lines below if you need to disable logging
# logging:
# driver: 'none' # disable saving logs
ports:
- 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/
First published: 27th July 2023