Profile picture for user Ivan Zugec
Ivan Zugec
18 Mar 2013

This is a guest post from Ivan Zugec.

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 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.

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 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.

Fig 1.4

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.

Profile picture for user Ivan Zugec

Ivan Zugec

Drupal developer

Ivan is the founder of Web Wash and spends most of his time consulting and writing about Drupal. He's been working with Drupal for more than 8 years and has successfully completed several large Drupal projects in Australia.

Contact us

Comments

Paul Querol 18 Mar 2013 17:21

I've found that a gotcha I've

I've found that a gotcha I've fallen into a couple of times when setting up sites is to set up a dummy or not yet existing site email address.

Receiving mail servers will often validate the senders address and should they find it not to exist, filter it out as spam.

It's one thing to check if the site appears to be sending but your not receiving the emails when testing.

Ivan Zugec 18 Mar 2013 23:28

In reply to by anonymous_stub (not verified)

If you need test email

If you need test email addresses, you could setup a catch-all email address. That way you could use whatever@domain.com or test-test@domain.com without having to create separate accounts.

Ivan Zugec 18 Mar 2013 23:48

In reply to by anonymous_stub (not verified)

Thanks.

Thanks.

Ivan Zugec 21 Mar 2013 10:22

In reply to by admin

I don't think there is a

I don't think there is a module, but you could setup a catch-all (http://en.wikipedia.org/wiki/Catch-all) email address.

A catch-all address is setup at the mail server.

Oscar Diaz 21 Mar 2013 10:43

What about faking incoming emails?

Thanks for you helpful post Ivan.
I wonder if there are any tricks to fake the reception of a certain email from a certain address?
I'm doing some work with the support ticketing system module (http://drupal.org/project/support) with 4 ticket types, and with several diferent locations (each with its own mailbox), and it can be a bit of a nightmare to create several test emails for several user roles (customer, support assistant, manager) for each location, and send and re-send the different tickets to check behaviour, to test my text parser, etc.
Such a module or trick would be veeery handy.

Thanks again!!

Oscar Diaz 21 Mar 2013 11:17

Thanks!

I had had a guess about what a catch-all email was when reading your post and comments, but it isn't until now, reading your answer and the wikipedia definition, when I see how it can be handy to my present case.
: )

Add new comment