How to inspect outgoing email content?
Question
There are several modules that send email from my site: Rules, Trigger, User, ...
I am looking for a place to set a drupal_set_message()
and print out the email contents. Where in the code would be the best place for that?
Solution
You can use hook_mail_alter()
to get (and alter) the contents of an e-mail message before it gets sent out:
function mymodule_mail_alter(&$message) {
dpm($message);
}
One thing to keep in mind is that not all modules use drupal_mail()
, the function that'll call hook_mail_alter()
, for various reasons. In that case, you'll have to identify where in the module's code the mail is sent out and see if they provide their own alter hooks: look for the functions drupal_alter('foo')
(implemented via hook_foo_alter()
), module_implements('foo')
, module_invoke('foo')
, or module_invoke_all('foo')
(all implemented by hook_foo()
).
Failing that, you may have to temporarily add the dpm()
call directly to the module in the right place.