Skip to main content

To view the email before it's sent from Drupal, especially when using Drupal 10 and dealing with encrypted emails or a specific module like Mailhandler, you can take a few steps to intercept or debug the email before it's actually dispatched. The process involves either configuring your configuration for debugging emails or using a development tool/module that captures outgoing emails.

 

Use Drupal's Mail System Configuration for Debugging

Drupal allows you to configure the mail system to use a different class for sending emails, which can be handy for debugging. You can switch to a 'null' or 'test' mailer that doesn't actually send the email but logs it or dumps it to the screen.

Install and configure a mail logger module: Modules like Devel or Maillog can be used to log emails instead of sending them. These modules provide options to log all mail sent by Drupal and usually offer a way to view the mail before it's encrypted and sent.

You can write a small module or use a hook_mail_alter() implementation in a custom module to alter or log the email before it's sent.  A basic example of how you can log the email details

/**
* Implements hook_mail_alter().
*/
function mymodule_mail_alter(&$message) {
 \Drupal::logger('mymodule')->notice('Mail ID: @id, Subject: @subject, Body: @body', [
   '@id' => $message['id'],
   '@subject' => $message['subject'],
   '@body' => is_array($message['body']) ? implode("\n", $message['body']) : $message['body'],
 ]);
}

 

Development and Testing Environments

For development and testing purposes, consider configuring your Drupal environment to send emails to a development service like Mailtrap, MailHog, or a similar service. These services capture outgoing emails and allow you to view them in a web interface, including headers, HTML content, and plain text.

Configure SMTP settings: Use the SMTP module to configure Drupal to send emails through a service like Mailtrap. This requires changing SMTP settings in your Drupal configuration to point to your Mailtrap inbox, allowing you to see all emails sent by Drupal in the Mailtrap dashboard.
Use MailHog: MailHog is a local SMTP server designed for development use. It captures emails sent by your Drupal site and makes them available in a web interface. You can run MailHog on your development machine or within your development environment.

 

Final Steps

After setting up one of the above methods, try triggering an email in your Drupal site that would normally be sent through Mailhandler. You should be able to view the email in the configured logging tool or email capture service before it's encrypted and sent, allowing you to see if there are any formatting issues or additional spaces being added.

Keep in mind that altering the mail system or using development tools like these should only be done in a development or staging environment to avoid disrupting email functionality in your live environment.

Related articles

Andrew Fletcher04 Apr 2025
Managing .gitignore changes
When working with Git, the .gitignore file plays a critical role in controlling which files and folders are tracked by version control. Yet, many developers are unsure when changes to .gitignore take effect and how to manage files that are already being tracked. This uncertainty can lead to...
Andrew Fletcher26 Mar 2025
How to fix the ‘Undefined function t’ error in Drupal 10 or 11 code
Upgrading to Drupal 10.4+ you might have noticed a warning in their code editor stating “Undefined function ‘t’”. While Drupal’s `t()` function remains valid in procedural code, some language analysis tools — such as Intelephense — do not automatically recognise Drupal’s global functions. This...
Andrew Fletcher17 Mar 2025
Upgrading to PHP 8.4 challenges with Drupal contrib modules
The upgrade from PHP 8.3.14 to PHP 8.4.4 presents challenges for Drupal 10.4 websites, particularly when dealing with contributed modules. While Drupal core operates seamlessly, various contrib modules have not yet been updated to accommodate changes introduced in PHP 8.4.x. This has resulted in...