Drupal coder

debugging

How to debug outgoing mails in Drupal?

There are a few issues involved when you are developing a Drupal website that needs to send out mails. When you need to test the sending of these e-mails you might want to see the results instantly without waiting for the actual mail to be delivered or you might want to test the mail with multiple addresses (instead of always your own) but you might not want to send out the actual mail. And in some cases you might not even have access to a mail server in your development environment.

As always, there are a few options to solve these issues in Drupal.

A better print_r for debugging purposes

We all use print_r from now to then in our code to quickly debug a statement. But it's easy to forget this and leave this in your code for unexperienced programmers. Sometimes it's in a conditional section and than it's very hard to keep track of these.
This information ruins your page and makes your clients very unhappy.

A better alternative is to use the dpr function, which is provided by the devel module. This function checks if a user has access to development information via a permission 'access devel information'.

And if you even want to top that off, do a function_exists to make sure you have devel installed (or some other module to provide this function).

$a = array('test');
if(function_exists('dpr')) dpr($a);