Drupal coder

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.

Logging outgoing mails

Your first option is to use the Mail Logger module.
This is a simple module that logs all outgoing messages to your database. You can then view the log of all sent mails in your reports section (Administer > Reports > Outgoing Mail log entries). For each individual mail you can consult the message body and all its corresponding headers.

This option is great if you don't have access to an mail server on you development machine. You also don't have to wait for the actual mail to be received in your mailbox since the log is written as soon as the mail is sent.

Mail Logger module screenshot

Rerouting mails to one (debugging) mail addres

Another option is to reroute all outgoing mails to your own (developer) address. This can be accomplished using the Reroute Email module.

The most important advantage of this one is that you're actually sending out any e-mails to any other mail addresses then your own.

reroute-mail.png

Writing your own logic with hook_mail_alter

If you want to do something else like logging the mails to syslog or add some more info to the logger, you can write your own module. Actually, the only trick the Mail Logger and Reroute Mail module use are hook_mail_alter.

This hook is called before sending out the actual mail and allows you to alter any details of your message like title, receiver, sender, ... This is also a great hook to do stuff with the data like writing logging it to the database, sending out a copy, etc.

March 24, 2010debugging, Drupal, module development

Comments

I found an interesting approach for debugging mails at http://drupal4hu.com/node/55
It's php script creating "fake" sendmail.

The Contact Save module is useful if you are having trouble receiving emails from your contact form. It stores them in the database (similar to Mail Logger), but has some additional features for the management of these messages.

Thx sun. The only issue with Devel is that it uses the watchdog. This combines all stuff in 1 log message field. That's a bit harder to debug then Mail Logger.

If it wasn't removed for some reason, then Devel module has a configuration setting that allows to log all e-mails built-in.

Another popular alternative is the PHPMailer module, which supports - admittedly not configurable via the UI - re-routing of all e-mails to an address defined in a system variable.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options