How to Test Outbound Email Functionality in Your Local Drupal Development Environment?

By Rinku Jacob | 18th April 2022 | 2 min read

Testing email functionality in Drupal is a common use case every developer encounters frequently. Setup a full-featured email system locally or using a 3rd party service for just testing emails may not be feasible always.

Mailhog is the best solution available for this scenario and the bonus point is that it works perfectly with Lando.

The general idea of Mailhog is that it acts as a local SMTP server that by default, doesn't send emails to the addressed recipients. Rather, it includes a clean UI that allows the developer to view outgoing emails. 

Installation

  • To use mailhog you have to install and configure SMTP module
  • Your lando should be configured to use mailhog as a service. Please check lando.yml and make sure that the service is available.
name: drupal
recipe: drupal9
services:
  appserver:
    webroot: web
  mail:
      type: mailhog
  • Once you start lando, check lando info for host and port information of 'mail' service of type 'mailhog'.
$lando info

{ service: 'mail',
    urls: [ 'http://localhost:49157' ],
    type: 'mailhog',
    healthy: true,
    hogfrom: [],
    internal_connection: { host: 'mail', port: '1025' },
    external_connection: { host: '127.0.0.1', port: 'not forwarded' },
    config: {},
    version: 'v1.0.1',
    meUser: 'mailhog',
    hasCerts: false,
    hostnames: [ 'mail.debug.internal' ] } ]
  • Set up these host as SMTP server and port as SMTP port in the SMTP module configuration.
SMTP Authentication Support.png
  • Open the url for mailhog ( You can get that in lando info) for the interface to see the emails sent from your site.
mailhog.png

Please note that the emails will be available in mailhog only for 24 hours by default.

Reference

https://www.drupaleasy.com/blogs/ultimike/2018/01/testing-local-drupal-site-emails-lando-and-mailhog