Managing Mail Handling for Development or Testing

Last modified: August 11, 2009 - 20:51

While debugging, testing or coding new features, if you want to see the content of the mails sent by your test Drupal site, your development platform should be properly set up so that mails are sent to a place where you will be able to find them.

Most of the notes that follow should be applicable to all platforms (Linux, Mac, Microsoft).

Drupal specific mail backend

Drupal can use an arbitrary mail backend. Look into http://api.drupal.org/api/function/drupal_mail_send/6 (in Drupal 6) or http://api.drupal.org/api/function/drupal_mail/5 (in Drupal 5). You can set the smtp_library variable to a file name, which Drupal will include and "send mail" with a drupal_mail_wrapper() function defined in that file. There you can log the mail to the database, a file or whatever. So all mail going out from Drupal can be nicely debugged locally. If you really need to send the mail over some server, as the name of the variable suggests, you can use an SMTP library and use any SMTP server you use otherwise to send mail, though I didn't have problems I solved this way though.

Reroute Email

A related module is reroute email. It intercepts all outgoing emails and reroutes them to an address you specify. This is very useful in development when you have a copy of a live site with thousands of users, and modules that send email (e.g. subscriptions), so email does not go to real users from the copy site. You can also add debug info in the email if you wish via a theme function.

Aliasing Your Existing Mail Account

When multiple email accounts are needed, many mail providers, such as gMail, allow for email aliasing. This allows for multiple Drupal accounts to be sent to a single mail account.

For example, if your email account is example@gMail.com, you can also receive email at example+user1@gMail.com, example+user2@gMail.com, etc.

This can also be used to redirect multiple real user email address to another account. This is useful if you have a dev or test environment that is a dump of your live environment with real user data. This SQL statement will update all your email addresses, so that no actual emails go out to your live users.

UPDATE users

SET mail = CONCAT('youremail+',CONCAT(REPLACE(mail, '@', '.'),'@gmail.com'))

Change "youremail" to the first part of the email address you plan to use. For example, if you want to send email to john@gmail.com, change youremail to john.

Shared Email Module

The shared email module overrides the 'user' module's validation that prevents the same email address being used by more than one user. Works for both registration and account updates. Displays a warning to the user that they are using a shared email.

PHP CLI Script

This is covered nicely by chx at http://www.drupal4hu.com/node/55 . In short: you set PHP to send mails to a file.

In some distributions of Linux, you have to make sure to install the package php-cli.

Mails to Logs

The Devel module ships with an smtp_library implementation which logs emails to the Watchdog.

Once the Devel module is installed, go to Administer -> Site configuration -> Devel settings. On that page there is a section titled SMTP library. Select 'Log only'. After sending an email from your site, go to Administer -> Reports -> Recent log entries. Among the log you should now see any outgoing messages. By clicking on the text under the 'Message' column you can see more details about that specific message.

Fakemail

Fakemail is a perl script that just works out of the box as long as you have perl installed. You just run the script, it listens on port 25, and dumps all mail received into a directory. You have no risk of spurious email being delivered to users by means of your sandbox.

Mail Transfer Agents

If you wish to have a more complete solution, you can use a mail transfer agent like Postfix, Exim or Sendmail.

Postfix

Postfix is a well-known and widely used free-and-open-source mail transfer agent.

To use Postfix, it must be installed on your server, or a server you have command-line access to. Installation and configuration of Postfix is beyond the scope of this page, however there is documentation available. Postfix runs on Linux, OSX, Unix or under Windows using Cygwin.

Here is the minimum configuration you will need: in /etc/postfix/main.cf configure

myhostname = <the hostname of your box> #if it is simply 'localhost', you can leave this line commented out.
inet_interfaces = localhost # so that it won't be an open relay
mydestination = $myhostname, localhost

You can find more complete documentation on how to configure Postfix on the Basic Postfix Configuration page.

To read emails using a command line reader such as the mail command, pine, elm, or mutt, a user account must be created on your server. In this example, the user would be "mailtest."

Then, to use this account, send mails to mailtest@ and read it using the command line reader.

Postfix can also relay messages to any mail account, such as gMail, Yahoo, Hotmail.

Exim

Exim is another well-known free and open source mail transfer agent. It runs on Linux or under Windows using Cygwin. Create a user account and read the mail using the mail command, pine, elm, mutt or other mail reader as described above. Exim can also relay messages to any mail account such as gMail, Yahoo, Hotmail, etc.

Sendmail

Sendmail is another powerful mail transfer agent, however it is often described as difficult to configure.

MailServe

On OSX, MailServe provides another option, although it is not free or open source.

Free SMTP Server

This Windows application provides an SMTP server.

Using Exim to reroute email

the greenman - June 24, 2009 - 11:11

One of the safest ways to manage email redirection for testing is to do it at your mail server level. There are unfortunately various modules that will bypass Drupal layer mail rerouting - which can be very embarrassing on a large site.

If you are using exim4, you can use this router (Set the email address to your own) to force all mail to go to one address:

################################################
# Redirect all outbound email to a specific address
# Useful on a testing/development box which should not contact the world

testingredirect:
  debug_print = "R: redirecting nonlocal for $local_part@$domain"
  driver = redirect
  domains = ! +local_domains
  allow_fail
  data = tests@example.com
  no_more

Make sure that this router gets run before any other smtp router. We have this in the debian conf.d directory as router/190_exim4-testing.
If you are on Debian, remember to regenerate and install your template.

> update-exim4.conf.template
> dpkg-reconfigure exim4-config

 
 

Drupal is a registered trademark of Dries Buytaert.