Using Mailhog via Docker For Development

Paulund - Apr 10 - - Dev Community

When you're developing your application locally you might want to send emails to users of your system.

In development you don't want to be sending real emails as you might accidentally be sending development emails to real customers. You need a way of testing locally and make sure that no emails will be sent out to customers but still allow you to view all emails being sent out.

This is why mailhog was created, it's a great development tool for testing emails and you can find the repository on Github.

Mailhog

You can run this locally on your machine by using the following commands

brew update && brew install mailhog
Enter fullscreen mode Exit fullscreen mode

This will setup SMTP on port 1025 and allow you to see these emails on port 8025.

But not every projects needs to have mailhog so you might not want this running all the time you can use docker to install mailhog instead to use it on the projects that actually need it.

Install With Docker

To install mailhog via docker you can use the following command

docker run -d -p 1025:1025 -p 8025:8025 mailhog/mailhog
Enter fullscreen mode Exit fullscreen mode

Now navigate to http://127.0.0.1:8025/ and you will see the mailhog interface.

To test this you can use telnet to submit a email.

telnet localhost 1025

EHLO test.com
MAIL FROM:<from@test.com>
RCPT TO:<to@test.com>
DATA
tags:
    - Hello World

Hello World!
.
QUIT
Enter fullscreen mode Exit fullscreen mode

You should now be able to see a test email in your mailhog interface.

Install With Docker Compose

If you would prefer to add this to docker compose you can do so by using the following script.

services:
  mailhog:
    image: mailhog/mailhog
    ports:
    - 1025:1025
    - 8025:8025
Enter fullscreen mode Exit fullscreen mode

Now when you run docker-compose up -d you will be able to see and use mailhog within this docker network.

This allows you to use Mailhog in any of your local development environments.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .