Email Logging in Drupal
There are a number of scenarios where you might want to log email being sent by Drupal; perhaps locally during development or for urgent debugging on a live site. There are a number of options available for Drupal email logging, and this article will cover four of the most common
- Using Devel module.
- Drupal logging modules: Maillog/Mail Developer, Mail Logger
- Drupal re-routing modules: Reroute Email, Advanced Mail Reroute
- SMTP server redirection
We'll look at the main pro's, con's and technical overhead involved in
each. I won't go into too much technical detail about setting things up, but
hopefully you'll get and idea of the options available when you next find
yourself wanting to log email from your Drupal site.
This is the simplest of the options, especially if you (like us) almost
invariably have the Devel module kicking around your
development Drupal install (if you're not familiar with devel, go check it
out; it's massively handy).
The steps involved:
- Download Devel from the project page and enable it.
- Visit the devel module configuration page.
- Set the 'SMTP library' option to 'log only'.
- View the emails in the watchdog at admin/reports/dblog
It should be noted that this method isn't really suitable for use on live
sites. Besides the fact that Devel isn't usually recommended for use in a
live environment, none of the emails will ever make it any further than
the log, meaning users won't get them, which is obviously bad news on a
live site. It should also be noted that this only works in the Drupal 6
version of Devel, as the functionality was dropped in the D7 version.
Drupal logging modules
There are several other modules available with the specific purpose of
logging emails within Drupal. Maillog for example allows you
to log all emails sent by the system, viewable at admin/settings/maillog.
Furthermore, you can optionally block emails from proceeding further,
which arguably makes it the most flexible for use in a live environment.
This is a nice simple solution when a log is the most helpful way to
Drupal re-routing modules
Sometimes you need more than a plain text log - debugging HTML email
for example isn't practical from within the Drupal interface. What's needed
in this case is viewing in an email client. There are again several modules
available that will help you achieve this. They will catch email leaving
Drupal and redirect to a given email address. For a wholesale approach,
there's Reroute Email, or for more complex routing rules
there's Advanced Mail Reroute.
Re-routing e-mails in this way will once again prevent the original
recipient from recieving the email, so is perhaps most useful during
development or if you are working with a live database dump and wish to
prevent emails getting to users.
The most complex but ultimately powerful solution is custom configuration
of a SMTP server on the machine the site is running on. On a unix
system, postfix (or other SMTP server) can be configured in any number of ways, to
block, redirect or BCC any outbound (or indeed inbound) e-mail. So during
development, you might configure your local machine to catch all
outbound e-mail sent by PHP and redirect it locally, for viewing in an
appropriately configured e-mail client of you choosing.
This big win of this is that you only need do this locally once, after which
you'll get all the e-mail sent by any of the sites you have set up on your
machine. Furthermore, in a UAT/QA or live context, the server could be
configured to silently BCC all outbound email to some defined address
(though be aware of any potential privacy/legal issues with doing so).
Server configuration is obviously the most powerful solution for Drupal
email logging, and elegant too, in that it doesn't touch your Drupal
codebase or change the execution of PHP at all. It would also work for any
other applications you have installed sending email via the server's built-in
SMTP libraries (should you also work in some other framework/CMS than
Drupal). It is however the most difficult to set up, requiring some
significant sysadmin chops to configure. Certainly tinkering with this in a
live environment would be best left to someone who knows what they're
Looking to Drupal 7
Depending on the environment, one of these solutions should be
appropriate to fit your Drupal e-mail logging needs. Presuming, that is,
you're still only working with Drupal 6. The only option currently
'ready' for Drupal 7 is the SMTP server configuration choice, whilst the
module closest to a stable release looks to be Reroute Email.