The Easiest Way To Debug Email Messages In Drupal

11 Aug · by Tim Kamanin · 2 min read

Time from time, while developing websites with Drupal I encounter the problem of debugging email messages my site sends. The common solution is to send test messages to your own mailbox to see how messages look like, debugging them this way. But this is not the most comfortable solution, moreover, it can become a nightmare debugging email on a localhost, especially on Windows, when you have to install local mail server, route all the stuff to localhost mailboxes and hassle with Mercury or some other thing...well in overall, it is not the most pleasant thing to do. Recently, I came to this simple and hassleless solution. Frankly, since I came up to this, mail debugging for Drupal isn't a nightmare to me anymore. So, enough words, it is time to act:

  1. Install a devel module (http://drupal.org/project/devel) for your version of Drupal and enable it. Chances, most of you have this already installed.
  2. Add hook_mail_alter to your custom module, like this:
/**
 * Implements hook_mail_alter().
 */
function module_name_mail_alter(&$message) {
  dpm($message);
}

Now, your module catches every email message Drupal sends and outputs it as _$message_ array in a nice Krumo view. As you see, this is even better than receiving real messages, you see every aspect of your outgoing email message including subject, body, mail headers and stuff. And this is without all the hassle with installing mail servers or other server related mumbo jumbo. Cool! If you don 't want email messages to go out of your website at all, modify the hook like this:

function module_name_mail_alter(&$message) {
  dpm($message);
  // We set 'To' field to nothing, so Drupal won't have any addressee
  $message['to'] = '';
}

~~No money, no honey~~ No addressee - no delivery. That 's simple! Hope this tip is helpful, don't be so shy and retweet it or facebook-like it to share with others. Have a nice day!

Comments

Required for comment verification



Kvark

Nice! That about read emails from mailbox?

Reply · 6 years, 4 months ago
Patrick Fournier

If you have the module devel installed, you can simply add $conf['mail_system'] = array( 'default-system' => 'DevelMailLog', ); to your site's settings.php. Mail will be written in your temporary directory, one file per email.

Reply · 6 years, 4 months ago