3 Modules giúp Debug Emails trong Drupal 7

Debugging email issues in Drupal can be really painful. Problems can exist in multiple areas; like Drupal itself or your mail server. So when an email issue arises you first need to check and see if Drupal is generating emails and then make sure your mail server is sending the emails. Luckily in Drupal there are a few modules that can lessen the pain of debugging emails.

In this article, I'll introduce you to a few modules that can help you debug email issues. More importantly these same modules can help you develop and test custom email functionality in Drupal.

Intercept Emails Using Devel Module

One useful way of debugging and testing emails is to use the custom mail class that ships with the Devel module. The mail class intercepts all outgoing emails and stores them in Drupal's temporary directory. The path of the temporary directory can be configured from the Configure -> File system page.

It’s important to note that none of the emails will actually be sent, they’ll instead be stored in the temporary directory.

Fig 1.0

Setting up Devel to log outgoing emails is pretty simple.

  1. First, make sure you download and install the Devel module.
  2. Add the code below into the settings.php.
$conf['mail_system'] = array(
'default-system' => 'DevelMailLog',
);

All we’re doing is telling Drupal to use the DevelMailLogclass as the default mail system.

3. Flush the site cache.

Once you have fired off a few emails go to the temporary directory, and you should see a folder called devel-mailsfull of logged emails.

Fig 1.1

Log Emails Using Mail Logger Module

The Mail Logger simply logs all outgoing emails into a database table. You can view the logged emails by going to the Reports -> Outgoing Mail log entries page. Just remember that emails will still be sent, this module just logs a copy of the emails.

Fig 1.2

Re-route All Emails Using Reroute Email Module

The Reroute Email module intercepts all outgoing emails and sends them to a configurable email address. This module is great for testing email notifications on a development or staging site. Any email that is generated by Drupal will be sent to predefined email addresses instead of end users.

Configuring the module is very easy.

1. Download and install the Reroute Email module.

2. Go to Configuration-> Reroute Email (admin/config/development/reroute_email).

3. Check the Enable rerouting checkbox and enter in an email address into the Email addresses text field.

Fig 1.3

Bonus: Debug and Modify Emails Using hook_mail_alter()

If you need to modify or debug an email as it’s being generated within Drupal, then look at implementing the hook_mail_alter() hook.

Install the Devel module and add the following code into a custom module. Replace EXAMPLEwith the module name.

/**
* Implements hook_mail_alter().
*/
function EXAMPLE_mail_alter(&$message) {
  dpm($message);
}

After you have sent off an email you should see the $messageprinted in the message area.

3 Modules giúp Debug Emails trong Drupal 7

Conclusion

Debugging an email issue is a simple two step process. First, make sure Drupal is generating the emails. Double check your code if you’re sending emails programmatically. Second, make sure the emails are being sent. This may require some digging through mail logs on the server.