PHPnews.io

Using MailHog via Docker for testing email

Written by Rob Allen / Original link on Oct. 21, 2020

I recently needed to modify the emails that a client project sends out. It was set up to send via SMTP and so the easiest way to do this for me was to add a local MailHog instance and point the application at it.

Manually running via Docker

The quickest and easiest way to do this is via Docker.

Manually, we can do:

$ docker run -p 8025:8025 -p 1025:1025 mailhog/mailhog

This will run MailHog with the SMTP port exposed on localhost port 1025 and the web interface on 8025.

Now you can configure the app's SMTP config and away you go.

Test via telnet

You can test via telnet fairly easily if you have it installed:

$ telnet localhost 1025

EHLO 19ft.com
MAIL FROM:
RCPT TO:
DATA
Subject: Hello World

Hello World!
.
QUIT

Type each line out and press Enter between each one and you'll see the new email in MailHog's web interface:

2020-09-29-mailhog.png

Docker compose

As I already had a Docker Compose setup for this project, I added the following to my docker-compose.yml:

services:
  mailhog:
    image: mailhog/mailhog
    logging:
      driver: 'none'  # disable saving logs
    ports:
      - 1025:1025 # smtp server
      - 8025:8025 # web ui

MailHog's logs are quite chatty, so adding driver: 'none' to the logging section will turn them off if you don't need them.

Done

That's it. I used to use Mailcatcher and still do on one project, but MailHog's Docker container makes it much easier and quicker to set up.

roballen roballen

« Using Laravel's Policies and Route Model Binding without Eloquent - Combine Multiple Field Tags in Go »