For debugging, I'd like to print the values being used to send e-mail messages to the screen.

After enabling the devel module, I tried this code (modified as per Letharion's comment):

function MY_MODULE_mail_alter(&$message) {
  dpm($message, 'email_test');

I don't get any error message, but I don't see any output, either. How can I print the value of '$message' to the screen?

More details about my site:

I'm trying to debug the e-mails sent upon user registration. There should be one sent by Drupal itself via the Logintoboggan module and another sent by the Rules module.

Anonymous users have the same permissions as admin to view debug messages; I tested this by adding a dpm() in my page.tpl.php.

I tried setting Display redirection page to on as per Clive's comment but the workflow is unchanged from before. If I add debug($message) to the MY_MODULE_mail_alter() then debug() will print the message from Drupal/logintoboggan but not the message from Rules.

  • 1
    Have you tried switching on the 'Display redirection page' option at site.com/admin/config/development/devel?
    – Clive
    Commented May 22, 2012 at 11:43
  • 1
    +1 Clive. Also, always do dpm($var, 'Some title'); when your output doesn't show. A blank (NULL, False) $message won't show up otherwise, and you won't know whether it's blank or if the code never executed.
    – Letharion
    Commented May 22, 2012 at 11:44
  • @Clive Thanks for the suggestion; I didn't know about that. Unfortunately, that didn't fix it; that may be because I am trying to print the user registration e-mail? Commented May 22, 2012 at 12:22
  • _user_email_notify uses drupal_mail so you should be able to output its data as any other outgoing mail. Perhaps trying to check module's weights could help ?
    – Countzero
    Commented May 23, 2012 at 8:03

1 Answer 1


The way I debug email is by using the TestingMailSystem class:

// Use testing mail system.
variable_set('mail_system', array('default-system' => 'TestingMailSystem'));

You can then capture and print the mail contents with the following statement:

$mails = $this->drupalGetMails();
debug($mails); // $mails is an array holding all mails sent.

Here $this is the current testing class (instance of DrupalWebTestCase).

This is very simple to use as part of automated testing (using the Testing framework that is part of the Drupal core).

If you don't want to be troubled by setting up the full Testing framework, an alternative method for debugging email is to hijack drupal_mail.

To do this you need to install the Devel module and edit the core mail.inc by replacing the standard drupal_mail with the following function:

 * Function to debug mail.
 function drupal_mail($module, $key, $to, $language, $params = array(), $from = NULL, $send = TRUE) {
  dpm('Using hacked version of drupal_mail.');
  dpm($params, 'mailbody');
  // Print out more arguments if required.
  return TRUE;

And yes, this is hacking core, so: 1) Don't do this on a production site; and 2) Remember to restore mail.inc with a pristine version when done debugging.

Note: The latter method is a rather nasty hack (but I've found it useful a few times). Using the Testing framework to debug mail is the recommended method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.