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 ( 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) {

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) {
  // 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!

Want to get more ๐Ÿ”ฅ tips like this one?

Subscribe to get notified about new dev tutorials