How to test email notifications in Drupal projects

Testing email notifications is often challenging because we don’t want to send out testing emails from development and local environments and confuse users. In addition to that, you may want to test headers, subject, body and the overall formatting applied to the information in the message and ensure that it’s readable.

There is an easy way to test email messages with the devel module, which consists of storing those emails as files in the server instead of sending them out.

Follow these steps to do that:

  1. Install and enable the devel module. You can use these drush commands:
    drush dl devel
    drush en devel
  2. Edit your settings file under `sites/default/settings.php`, and add these lines of code:
    $conf['mail_system'] = array(
     'default-system' => 'DevelMailLog',
    );
    
    $conf['devel_debug_mail_directory'] = '/path/to/email/files';

Once that has been set up, generate some email submissions and then go to the path you specified in the `$conf[‘devel_debug_mail_directory’]` variable. You will see some files like these:

[email protected] ~/dev-html/drupal/docroot/sites/default/files/mails (master) $ ls -la
total 72
drw-r--r-- 2 apache apache 4096 Aug 21 16:04 .
drw-r--r-- 11 apache apache 4096 Aug 17 15:49 ..
-rw-r--r-- 1 apache apache 334 Aug 17 17:02 [email protected]_Contact_Notification-17-08-17_050201.mail.txt
-rw-r--r-- 1 apache apache 303 Aug 17 17:30 [email protected]_Contact_Notification-17-08-17_053022.mail.txt
-rw-r--r-- 1 apache apache 337 Aug 17 17:17 [email protected]_Contact_Notification-17-08-17_051721.mail.txt
-rw-r--r-- 1 apache apache 314 Aug 17 17:29 [email protected]_Contact_Notification-17-08-17_052948.mail.txt
-rw-r--r-- 1 apache apache 499 Aug 18 17:54 [email protected]_Event_Registration-17-08-18_055458.mail.txt
-rw-r--r-- 1 apache apache 504 Aug 18 18:04 [email protected]_Event_Registration-17-08-18_060456.mail.txt
-rw-r--r-- 1 apache apache 534 Aug 18 18:24 [email protected]_Event_Registration-17-08-18_062445.mail.txt
-rw-r--r-- 1 apache apache 510 Aug 18 18:25 [email protected]_Event_Registration-17-08-18_062548.mail.txt

Then, you can test the headers and content of the messages by simply checking those files:

[email protected] ~/dev-html/drupal/docroot/sites/default/files/mails (master) $ more [email protected]_Event_Registration-17-08-18_055458.mail.txt
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed; delsp=yes
Content-Transfer-Encoding: 8Bit
X-Mailer: Drupal
Return-Path: [email protected]
Sender: [email protected]
From: [email protected]
To: [email protected]
Subject: New Event Registration

First Name: Luis
Last name: Cuellar
Title: my title
Organization: Agileana
Organization Category: Academia
Phone Number: 202.43434.343
Email Address: [email protected]
Country: United States
State: AL

Supported Topics: 
 - Topic 1
 - Topic 2