diff --git includes/mail.inc includes/mail.inc
index 33e8436..8cf7c77 100644
--- includes/mail.inc
+++ includes/mail.inc
@@ -127,7 +127,7 @@ function drupal_mail($module, $key, $to, $language, $params = array(), $from = N
 
   // Optionally send e-mail.
   if ($send) {
-    $message['result'] = drupal_mail_send($message);
+    $message['result'] = drupal_send($module, $key)->mail($message);
 
     // Log errors
     if (!$message['result']) {
@@ -140,43 +140,43 @@ function drupal_mail($module, $key, $to, $language, $params = array(), $from = N
 }
 
 /**
- * Send an e-mail message, using Drupal variables and default settings.
- * More information in the <a href="http://php.net/manual/en/function.mail.php">
- * PHP function reference for mail()</a>. See drupal_mail() for information on
- * how $message is composed.
- *
- * @param $message
- *  Message array with at least the following elements:
- *   - id
- *      A unique identifier of the e-mail type. Examples: 'contact_user_copy',
- *      'user_password_reset'.
- *   - to
- *      The mail address or addresses where the message will be sent to. The
- *      formatting of this string must comply with RFC 2822. Some examples are:
- *       user@example.com
- *       user@example.com, anotheruser@example.com
- *       User <user@example.com>
- *       User <user@example.com>, Another User <anotheruser@example.com>
- *   - subject
- *      Subject of the e-mail to be sent. This must not contain any newline
- *      characters, or the mail may not be sent properly.
- *   - body
- *      Message to be sent. Accepts both CRLF and LF line-endings.
- *      E-mail bodies must be wrapped. You can use drupal_wrap_mail() for
- *      smart plain text wrapping.
- *   - headers
- *      Associative array containing all mail headers.
- * @return
- *   Returns TRUE if the mail was successfully accepted for delivery,
- *   FALSE otherwise.
+ * The default Drupal mail sending library using PHP's mail function.
  */
-function drupal_mail_send($message) {
-  // Allow for a custom mail backend.
-  if (variable_get('smtp_library', '') && file_exists(variable_get('smtp_library', ''))) {
-    include_once DRUPAL_ROOT . '/' . variable_get('smtp_library', '');
-    return drupal_mail_wrapper($message);
-  }
-  else {
+class DrupalMailSend implements DrupalMailSendingInterface {
+  /**
+   * Send an e-mail message, using Drupal variables and default settings.
+   * More information in the <a href="http://php.net/manual/en/function.mail.php">
+   * PHP function reference for mail()</a>. See drupal_mail() for information on
+   * how $message is composed.
+   *
+   * @param $message
+   *  Message array with at least the following elements:
+   *   - id
+   *      A unique identifier of the e-mail type. Examples: 'contact_user_copy',
+   *      'user_password_reset'.
+   *   - to
+   *      The mail address or addresses where the message will be sent to. The
+   *      formatting of this string must comply with RFC 2822. Some examples are:
+   *       user@example.com
+   *       user@example.com, anotheruser@example.com
+   *       User <user@example.com>
+   *       User <user@example.com>, Another User <anotheruser@example.com>
+   *   - subject
+   *      Subject of the e-mail to be sent. This must not contain any newline
+   *      characters, or the mail may not be sent properly.
+   *   - body
+   *      Message to be sent. Accepts both CRLF and LF line-endings.
+   *      E-mail bodies must be wrapped. You can use drupal_wrap_mail() for
+   *      smart plain text wrapping.
+   *   - headers
+   *      Associative array containing all additional mail headers not defined
+   *      by one of the other parameters.  PHP's mail() looks for Cc and Bcc
+   *      headers and sends the mail to addresses in these headers too.
+   * @return
+   *   Returns TRUE if the mail was successfully accepted for delivery,
+   *   FALSE otherwise.
+   */
+  public function mail($message) {
     $mimeheaders = array();
     foreach ($message['headers'] as $name => $value) {
       $mimeheaders[] = $name . ': ' . mime_header_encode($value);
@@ -195,6 +195,142 @@ function drupal_mail_send($message) {
 }
 
 /**
+ * A dummy mail sending library that captures sent messages to a variable.
+ *
+ * This class is for running tests or for development.
+ */
+class DrupalTestMailSend implements DrupalMailSendingInterface {
+
+  /**
+   * Accept an e-mail message and store it in a variable.
+   *
+   * @param  $message
+   *   An e-mail message. See drupal_mail() for information on how $message is composed.
+   */
+  public function mail($message) {
+    $captured_emails = variable_get('drupal_test_email_collector', array());
+    $captured_emails[] = $message;
+    variable_set('drupal_test_email_collector', $captured_emails);
+  }
+}
+
+/**
+ * A mail sending class for suppressing certain emails.
+ */
+class DrupalDevNullMailSend implements DrupalMailSendingInterface {
+  /**
+   * Accept an e-mail message and do nothing.
+   *
+   * @param  $message
+   *   An e-mail message.
+   */
+  public function mail($message) {
+    // Throw the message away.
+  }
+}
+
+/**
+ * Returns an object that implements DrupalMailSendingInterface.
+ *
+ * Allows for one or more custom mail backends to send mail messages
+ * composed using drupal_mail().
+ * 
+ * The selection of a particular implementation is controlled via the variable
+ * 'mail_sending_system', which is a keyed array.  The default class name
+ * is taken from the value corresponding to the 'default-system' key. To use
+ * a different system for all mail sent by one module, specify also a class
+ * name as the value for the key corresponding to the module name.  For example
+ * to debug all mail sent by the user module by logging it to a file, you
+ * might set the variable as something like:
+ *
+ * array(
+ *   'default-system' => 'DrupalMailSend',
+ *   'user' => 'DevelMailLog',
+ * );
+ *
+ * Finally, a different system can be specified for a specific e-mail ID (see
+ * the $key param), such as one of the keys used by the contact module:
+ *
+ * array(
+ *   'default-system' => 'DrupalMailSend',
+ *   'user' => 'DevelMailLog',
+ *   'contact_page_autoreply' => 'DrupalDevNullMailSend',
+ * );
+ *
+ * @param $module
+ *   The module name which was used by drupal_mail() to invoke hook_mail().
+ * @param $key
+ *   A key to identify the e-mail sent. The final e-mail ID for the e-mail
+ *   alter hook in drupal_mail() would have been {$module}_{$key}.
+ */
+function drupal_send($module, $key) {
+  $instances = &drupal_static(__FUNCTION__, array());
+
+  $id = $module . '_' . $key;
+  $configuration = variable_get('mail_sending_system', array('default-system' => 'DrupalMailSend'));
+
+  // Look for overrides for the default class, starting from the most specific
+  // id, and falling back to the module name.
+  if (isset($configuration[$id])) {
+    $class = $configuration[$id];
+  }
+  elseif (isset($configuration[$module])) {
+    $class = $configuration[$module];
+  }
+  else {
+    $class = $configuration['default-system'];
+  }
+
+  if (empty($instances[$class])) {
+    $interfaces = class_implements($class);
+    if (isset($interfaces['DrupalMailSendingInterface'])) {
+      $instances[$class] = new $class;
+    }
+    else {
+      throw new Exception(t('Class %class does not implement interface %interface', array('%class' => $class, '%interface' => 'DrupalMailSendingInterface')));
+    }
+  }
+  return $instances[$class];
+}
+
+/**
+ * An interface for pluggable mail back-ends.
+ */
+interface DrupalMailSendingInterface {
+  /**
+   * Send an e-mail message composed by drupal_mail().
+   *
+   * @param $message
+   *  Message array with at least the following elements:
+   *   - id
+   *      A unique identifier of the e-mail type. Examples: 'contact_user_copy',
+   *      'user_password_reset'.
+   *   - to
+   *      The mail address or addresses where the message will be sent to. The
+   *      formatting of this string must comply with RFC 2822. Some examples are:
+   *       user@example.com
+   *       user@example.com, anotheruser@example.com
+   *       User <user@example.com>
+   *       User <user@example.com>, Another User <anotheruser@example.com>
+   *   - subject
+   *      Subject of the e-mail to be sent. This must not contain any newline
+   *      characters, or the mail may not be sent properly.
+   *   - body
+   *      Message to be sent. Accepts both CRLF and LF line-endings.
+   *      E-mail bodies must be wrapped. You can use drupal_wrap_mail() for
+   *      smart plain text wrapping.
+   *   - headers
+   *      Associative array containing all additional mail headers not defined
+   *      by one of the other parameters.  Mail will also be sent to addresses
+   *      in Cc and Bcc headers.
+   * @return
+   *   Returns TRUE if the mail was successfully accepted for delivery,
+   *   FALSE otherwise.
+   */
+   public function mail($message);
+}
+
+/**
  * Perform format=flowed soft wrapping for mail (RFC 3676).
  *
  * We use delsp=yes wrapping, but only break non-spaced languages when
diff --git modules/simpletest/drupal_web_test_case.php modules/simpletest/drupal_web_test_case.php
index 7370c3c..4cf768c 100644
--- modules/simpletest/drupal_web_test_case.php
+++ modules/simpletest/drupal_web_test_case.php
@@ -1102,9 +1102,8 @@ class DrupalWebTestCase extends DrupalTestCase {
     unset($GLOBALS['conf']['language_default']);
     $language = language_default();
 
-    // Make sure our drupal_mail_wrapper function is called instead of the
-    // default mail handler.
-    variable_set('smtp_library', drupal_get_path('module', 'simpletest') . '/drupal_web_test_case.php');
+    // Use the test mail class instead of the default mail handler class.
+    variable_set('mail_sending_system', array('default-system' => 'DrupalTestMailSend'));
 
     // Use temporary files directory with the same prefix as database. The
     // directory will have been created already.
@@ -1155,7 +1154,7 @@ class DrupalWebTestCase extends DrupalTestCase {
     simpletest_log_read($this->testId, $db_prefix, get_class($this), TRUE);
     $db_prefix = $db_prefix_temp;
 
-    $emailCount = count(variable_get('simpletest_emails', array()));
+    $emailCount = count(variable_get('drupal_test_email_collector', array()));
     if ($emailCount) {
       $message = format_plural($emailCount, t('!count e-mail was sent during this test.'), t('!count e-mails were sent during this test.'), array('!count' => $emailCount));
       $this->pass($message, t('E-mail'));
@@ -1909,7 +1908,7 @@ class DrupalWebTestCase extends DrupalTestCase {
    *   An array containing e-mail messages captured during the current test.
    */
   protected function drupalGetMails($filter = array()) {
-    $captured_emails = variable_get('simpletest_emails', array());
+    $captured_emails = variable_get('drupal_test_email_collector', array());
     $filtered_emails = array();
 
     foreach ($captured_emails as $message) {
@@ -2466,7 +2465,7 @@ class DrupalWebTestCase extends DrupalTestCase {
    *   TRUE on pass, FALSE on fail.
    */
   protected function assertMail($name, $value = '', $message = '') {
-    $captured_emails = variable_get('simpletest_emails', array());
+    $captured_emails = variable_get('drupal_test_email_collector', array());
     $email = end($captured_emails);
     return $this->assertTrue($email && isset($email[$name]) && $email[$name] == $value, $message, t('E-mail'));
   }
@@ -2486,22 +2485,7 @@ class DrupalWebTestCase extends DrupalTestCase {
       $this->pass(l(t('Verbose message'), $this->originalFileDirectory . '/simpletest/verbose.html', array('fragment' => $id)), 'Debug');
     }
   }
-}
-
-/**
- * Wrapper function to override the default mail handler function.
- *
- * @param  $message
- *   An e-mail message. See drupal_mail() for information on how $message is composed.
- * @return
- *   Returns TRUE to indicate that the e-mail was successfully accepted for delivery.
- */
-function drupal_mail_wrapper($message) {
-  $captured_emails = variable_get('simpletest_emails', array());
-  $captured_emails[] = $message;
-  variable_set('simpletest_emails', $captured_emails);
 
-  return TRUE;
 }
 
 /**
diff --git modules/simpletest/simpletest.info modules/simpletest/simpletest.info
index 28bbef3..a53c9b0 100644
--- modules/simpletest/simpletest.info
+++ modules/simpletest/simpletest.info
@@ -23,6 +23,7 @@ files[] = tests/filetransfer.test
 files[] = tests/form.test
 files[] = tests/graph.test
 files[] = tests/image.test
+files[] = tests/mail.test
 files[] = tests/menu.test
 files[] = tests/module.test
 files[] = tests/registry.test
diff --git modules/simpletest/tests/mail.test modules/simpletest/tests/mail.test
new file mode 100644
index 0000000..6ee6dde
--- /dev/null
+++ modules/simpletest/tests/mail.test
@@ -0,0 +1,57 @@
+<?php
+// $Id$
+
+/**
+ * Test the Drupal mailing system.
+ */
+class MailTestCase extends DrupalWebTestCase implements DrupalMailSendingInterface {
+  /**
+   * The most recent message that was sent through the test case.
+   *
+   * We take advantage here of the fact that static variables are shared among
+   * all instance of the same class.
+   */
+  private static $sent_message;
+
+  /**
+   * Implementation of getInfo().
+   */
+  function getInfo() {
+    return array(
+      'name' => t('Mail system'),
+      'description' => t('Performs tests on the pluggable mailing framework.'),
+      'group' => t('System'),
+    );
+  }
+
+  /**
+   * Implementation of setUp(). 
+   */
+  function setUp() {
+    parent::setUp();
+
+    // Add MailTestCase as an SMTP library
+    variable_set('mail_sending_system', array('default-system' => 'MailTestCase'));
+  }
+
+  /**
+   * Assert that the pluggable mail system is functional.
+   */
+  function testPluggableFramework() {
+    global $language;
+
+    // Use MailTestCase for sending a message.
+    $message = drupal_mail('simpletest', 'mail_test', 'testing@drupal.org', $language);
+
+    // Assert whether the message was sent through the send function.
+    $this->assertEqual(self::$sent_message['to'], 'testing@drupal.org', t('Pluggable mail system is extendable.'));
+  }
+
+  /**
+   * Send function that is called through the mail system.
+   */
+  public function mail($message) {
+    self::$sent_message = $message;
+  }
+}
+
