Community Documentation

Managing Mail Handling for Development or Testing

Last updated September 6, 2012. Created by rfay on December 18, 2007.
Edited by Matt V., schultetwin, wizonesolutions, scor. Log in to edit this page.

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.

Installing and configuring the SMTP Authentication Support module is an easy way to allow Drupal to bypass the PHP mail() function and send email directly to an SMTP server. The module supports SMTP authentication and can even connect to servers using SSL if supported by PHP.

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, '@', '.AT.'),'@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.

This is known to work for Google Apps Mail (which is Gmail) as well. In that case, you should change "gmail.com" to your own domain. If you receive email at admin@example.com, then your domain is example.com.

If you use Drush, then you can simply run the following from your site directory.

drush sqlq "UPDATE users SET mail = CONCAT('youremail+', CONCAT(REPLACE(mail, '@', '.AT.'), '@gmail.com'))"

Make sure that you do this on the right database! You can check the database Drush will use by running drush sql-conf

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.

Mail to Log

The Drupal 6 version of 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.

Mail to Temp Files

The watchdog logging functionality was removed in the Drupal 7 version of the Devel module, in favor of logging to temporary files instead. To enable temp file logging, add the following to your site's settings.php file:

$conf['mail_system'] = array(
  'default-system' => 'DevelMailLog',
);

Each individual outgoing message will then be written to its own file in the temporary directory designated for the site (typically /tmp). More information can be found in the documentation for DevelMailLog.

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.

Third Party Solutions

mailtrap.io

mailtrap.io - This is a free (for the present time at least, I don't know if they plan to have people start paying later) service that allows you to configure postfix/phpmailer/whatever you use to send mail to mailtrap.io instead. Mailtrap will buts it in it's inbox on their servers. Allows forwarding to personal e-mail addresses as well as giving "inbox" access to other mailtrap.io users so that multiple people can view the same mailbox.

Comments

Using Exim to reroute email

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

About this page

Drupal version
Drupal 5.x, Drupal 6.x, Drupal 7.x
Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here