Dealing With Email On Drupal Development Sites

Old mail trucks

Sometimes, even when you've worked with a system for a long time, you can still uncover surprising things that have been around for awhile but were previously unknown to you.

Such was the case for the Reroute Email module. Maybe you've already heard of it, but I had not. In fact, for years, I've dreaded working on email issues for existing sites with notification systems, fearing I would spam live site users. In at least one case, I've sent an inadvertent content update notification to a not-so-small user population. Fortunately, I wasn't using Samuel L Ipsum to generate dummy content or things could have gotten much more uncomfortable.

Recently, I had the great idea of writing a module to intercept site emails and redirect them to a different, safer address. I followed this with the thought, "I wonder if a module like that already exists," and as is so often the case with Drupal, the answer was, "of course." In fact, this functionality has been around since the days of Drupal 5, and appears to have been part of the devel module before splitting off on its own. That's a long time to fly under my radar, especially considering the are nearly 375,000 reported downloads.

Why You Might Want To Use It

Many sites use notification systems to send emails to users when you change existing content or publish new content. Some sites send out notification emails to users as content goes through a moderation workflow. A site may send out emails to e-commerce customers or in response to contact form submissions. The list goes on and on.

What happens, though, when you pull the database down to your local development environment? Maybe you were able to sanitize the database with Drush when you pulled it, or maybe not. You may think your sanitization caught every email address on your site, but it probably didn't (think Rules or contact form or update notifications). Are you really going to remember to change all those email addresses every time you sync the database? Maybe the first time and the second time, maybe even the third or fourth time. If you're like most people though, you'll eventually decide that it's not necessary, since you're "not working on email right now; plus, it's such a pain."

That's where Reroute Email comes in.

Installation and Configuration

I'm assuming you already know how to install a Drupal module, so I'll spare you those particular details. The main idea is to configure things so Reroute Email is automatically setup in environments where you need it. In this case, we're talking about your local development environment.

The first thing to do is to ensure that the module gets enabled whenever you synchronize the database locally. This is easy if you use drush and sql-sync or sql-sync-pipe. Just add a target-command-specific element to your Drush alias:

'target-command-specific' => array(
 'sql-sync' => array(
   'enable' => array('reroute_email'),
 'sql-sync-pipe' => array(
   'enable' => array('reroute_email'),

After that, it's just a matter of setting variables in your local settings.php to configure it the way you want:

// This setting allows you to defeat Reroute Email even if 
// it's enabled in the Drupal system sense, so make sure it's
// set to "1"
$conf['reroute_email_enable'] = 1;

// Specify where you want to redirect emails. You can 
// define more than one email by separating them with
// spaces, commas or semicolons
$conf['reroute_email_address'] = '';

// Adds an extra message to the body with information
// about the redirection
$conf['reroute_email_enable_message'] = 1;

Keep in mind that if you set these values in your local settings.php, you cannot override them by changing them in the UI (i.e., Admin >> Configuration >> Development >> Reroute Email).

You can send a test email through the module from Admin >> Configuration >> Development >> Reroute Email >> Test email form.


There is always a caveat, isn't there? Reroute Email uses hook_mail_alter() to do its work, so if any of your modules send mail in any way except through drupal_mail() (e.g., by calling drupal_mail_system()->mail() or PHP mail() directly), they will bypass this hook and thus not get redirected.

Of course, since it uses hook_mail_alter(), Reroute Email should work with any standard Drupal mail mechanism, whether you're using just the default Drupal mail or something more complex, like Webform with MailSytem, MimeMail and SMTP Authentication.

Note that if your site is setup with the default Drupal mail, you may also need to setup outgoing mail on your local development environment. It doesn't seem like this is something that would be that difficult, but it can get quite tricky.

Do One Thing Well

Reroute Email is a small module that has been around for a long time. It does one really useful thing and it does it simply and well. If only we could say that about more Drupal modules.