Testing local Drupal 10 outbound emails with Lando, Drupal Symfony Mailer, and Mailhog.
If you are a Drupal Developer, testing email functionality should sound familiar, because it is a common task that we have to perform to make sure that every email integration is working properly.
The issue comes up when developers need to test it on local or development environments due to in some cases, the website is going to send these outbound email tests to recipients' emails that are real users of our website. Of course, we don’t want that.
Mailhog is a solution available for this scenario, it makes it easy to test outgoing emails from a local development environment. The general idea of Mailhog is that it acts as a local email server by default, and doesn't send emails to the addressed recipients. Also, it includes a UI that allows the developers to view outgoing emails.
On the other hand, We use Lando to manage our local environments for Drupal, WordPress, and Backdrop projects. It's a free, open-source, cross-platform, local development environment and DevOps tool built on Docker container technology. It allows us to set up local environments in an easy way.
Additionally, for this example, we use the Drupal Symfony Mailer module to manage the mail system on Drupal, it is based on the popular Symfony Mailer library giving full support of HTML emails, file attachments, embedded images, 3rd-party delivery integrations, async sending and more.
Let’s put it all together!
To install Mailhog in an existing Lando site, simply add the following to your .lando.yml
If you want to change the URL of the Mailhog UI, just change it from “drupal10-mailhog.lndo.site” to something specific to your project.
Then, run "lando rebuild". After it, you are just about done, you will see a new set of URLs for the local Mailhog UI, similar to the next screenshot
On the Drupal side, you will need to install/enable the Drupal Symfony Mailer module. We use Composer to manage Drupal dependencies, so to install this module
- Run the command line “lando composer require 'drupal/symfony_mailer:^1.2'”
- To enable it please run “lando drush en symfony_mailer”
Before testing our Mailer on our Drupal website, we might need to fix an issue, probably if you go to your local website visiting the URL “/admin/config/system/mailer/test” and try it, you will get an error “Connection to "process /usr/sbin/sendmail -bs" has been closed unexpectedly.”
I found a solution for this problem, according to comment #8 from an issue reported for Drupal Symfony Mailer, we should add the next line in our “settings.local.php” file.
“$config['symfony_mailer.mailer_transport.sendmail']['configuration']['query']['command'] = ini_get('sendmail_path') . ' -t';”
Note: If you haven't done it already, you might enable your settings.local.php file so you can have your local development overrides.
Finally, we should be able to test the mailer system using Mailhog, using your web browser go to [your-local-domain]/admin/config/system/mailer/test and fill the Recipient field with the email address that you want and click the Send button, See the next screenshot:
Then go to your local Mailhog URL in my case “http://drupal10-mailhog.lndo.site/” and you should see the next UI
Congratulations! You are ready to test the outbound emails locally using Lando, Drupal Symfony Mailer, and Mailhog.
Pig icons created by Freepik - FlaticonBack to blogs