Test your Drupal emails with Lando, MailHog, and Swiftmailer

MailHog Drupal email

If you haven't heard of MailHog, it is an email testing tool for developers. Lando has native support for MailHog, you just need to add it as a service to your .lando.yml file. You can just copy the example I leave below, which also has some cool extras, like enabling and disabling Xdebug on the fly. There's a second file .lando.php.ini that .lando.yml references with some custom settings, like changing the default xdebug.remote_port to 9001 (I need that on my Mac).

On the Drupal side, you'll obviously need to enable the Swift Mailer module. If you haven't done it already, you should enable your settings.local.php file so you can have your local development overrides.

.lando.yml

name: drupal
recipe: drupal8
config:
  webroot: web
  xdebug: false
  database: mysql:8.0
  config:
    php: .lando.php.ini
services:
  phpmyadmin:
    type: phpmyadmin
    hosts:
      - database
  appserver:
    overrides:
      ports:
        - "0.0.0.0::80"
      environment:
        PHP_SENDMAIL_PATH: '/usr/sbin/sendmail -S mailhog:1025'
  mailhog:
    type: mailhog
    portforward: true
    hogfrom:
      - appserver
proxy:
  mailhog:
    - mail.lndo.site
tooling:
  xdebug-on:
    service: appserver
    description: Enable xdebug for apache.
    cmd: "docker-php-ext-enable xdebug && /etc/init.d/apache2 reload"
    user: root
  xdebug-off:
    service: appserver
    description: Disable xdebug for apache.
    cmd: "rm /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && /etc/init.d/apache2 reload"
    user: root

.lando.php.ini

; Xdebug
xdebug.max_nesting_level = 256
xdebug.show_exception_trace = 0
xdebug.collect_params = 0
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_host = ${LANDO_HOST_IP}
xdebug.remote_port=9001

settings.local.php

// Swiftmailer MailHog settings override
$config['swiftmailer.transport']['transport'] = 'smtp';
$config['swiftmailer.transport']['smtp_host'] = 'mailhog';
$config['swiftmailer.transport']['smtp_port'] = '1025';
$config['swiftmailer.transport']['smtp_encryption'] = '0';

 

By José Fernandes