Khanh Hoang - Kenn
Kenn is a user experience designer and front end developer who enjoys creating beautiful and usable web and mobile experiences.
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.
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.
Setting up Devel to log outgoing emails is pretty simple.
settings.php
.$conf['mail_system'] = array(
'default-system' => 'DevelMailLog',
);
All we’re doing is telling Drupal to use the DevelMailLog
class 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-mails
full of logged emails.
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.
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.
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 EXAMPLE
with 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 $message
printed in the message area.
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.