How to use MailHog in Lando

If you’ve ever needed a service to capture e-mails, MailHog is a pretty good choice. MailHog contains an SMTP server which you send messages to, simulating your production SMTP service, and provides a HTTP interface so you can see what’s being sent.

A post box with frost around its edges

Photo by Charlotte Harrison on Unsplash.

It isn’t a substitute for your production mail service or e-mail client, so you’ll still need to plug it all in for testing, and make sure to check in a variety of e-mail clients. This is good if you need to debug why e-mail clients are rendering your messages strangely and need to experiment.

With that in mind, I’ll show you how to add a MailHog service in Lando via the Landofile .lando.yml.

Prerequisites

  • You know how to run Lando
  • You have an application which sends messages via SMTP

Setting up your Landofile

In your Landofile (.lando.yml), add a new section for services. If your Landofile already has one, you can append to it instead. Below I define a new service called mail:

# .lando.yml
# ...the rest of the YAML config sits above here

services:
  mail:
    type: mailhog

We’re almost ready to go, but we need to provide information about which services need to be able to access MailHog. To do that, define a key labelled hogfrom, and list all our services. In this case, appserver is responsible for sending our message:

# .lando.yml
# ...the rest of the YAML config sits above here

services:
  mail:
    type: mailhog
    hogfrom:
      - appserver

Run lando rebuild, which should rebuild your stack, including the new MailHog service. In the output where you see a list of URLs you can access your app from, you should now see a localhost URL that you can open to access the MailHog interface.

Configuring your web app

In your web app, you will need to change the SMTP configuration so it points at MailHog. In Drupal, this can be done by overriding the system configuration in settings.local.php. This may differ depending on your specific application, so I can’t help with that.

In terms of credentials and URLs, here is what you can use to connect to MailHog’s SMTP server:

  • Hostname: mail—same as the name we gave the service above
  • Port: 1025
  • Username: as desired
  • Password: as desired

Some questions

Couldn’t I just run MailHog outside of Lando or Docker?

Sure, but Lando exists as a way to lock down those tools so your colleagues don’t have to figure out how to install MailHog, or ask you how to install it.

Wasn’t MailHog development suspended?

It was for some time, but development picked up again and a new release 1.0.1 was put out August 12 2020. I was a bit surprised as well, since the last official release 1.0.0 was in 2017.