Index: includes/mail.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/mail.inc,v
retrieving revision 1.17
diff -u -p -r1.17 mail.inc
--- includes/mail.inc	7 Nov 2008 16:59:36 -0000	1.17
+++ includes/mail.inc	11 Nov 2008 03:57:53 -0000
@@ -9,7 +9,7 @@
  * appropriate places in the template. Processed e-mail templates are
  * requested from hook_mail() from the module sending the e-mail. Any module
  * can modify the composed e-mail message array using hook_mail_alter().
- * Finally drupal_mail_send() sends the e-mail, which can be reused
+ * Finally drupal_send()->mail() sends the e-mail, which can be reused
  * if the exact same composed e-mail is to be sent to multiple recipients.
  *
  * Finding out what language to send the e-mail with needs some consideration.
@@ -56,7 +56,7 @@
  *   called to complete the $message structure which will already contain common
  *   defaults.
  * @param $key
- *   A key to identify the e-mail sent. The final e-mail id for e-mail altering
+ *   A key to identify the e-mail sent. The final e-mail ID for e-mail altering
  *   will be {$module}_{$key}.
  * @param $to
  *   The e-mail address or addresses where the message will be sent to. The
@@ -72,7 +72,7 @@
  * @param $from
  *   Sets From to this value, if given.
  * @param $send
- *   Send the message directly, without calling drupal_mail_send() manually.
+ *   Send the message directly, without a manual call to drupal_send()->mail().
  * @return
  *   The $message array structure containing all details of the
  *   message. If already sent ($send = TRUE), then the 'result' element
@@ -127,7 +127,7 @@ function drupal_mail($module, $key, $to,
 
   // 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']) {
@@ -139,11 +139,104 @@ function drupal_mail($module, $key, $to,
   return $message;
 }
 
+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 mail headers.
+   * @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);
+    }
+    return mail(
+      $message['to'],
+      mime_header_encode($message['subject']),
+      // Note: e-mail uses CRLF for line-endings, but PHP's API requires LF.
+      // They will appear correctly in the actual e-mail that is sent.
+      str_replace("\r", '', $message['body']),
+      // For headers, PHP's API suggests that we use CRLF normally,
+      // but some MTAs incorrecly replace LF with CRLF. See #234403.
+      join("\n", $mimeheaders)
+    );
+  }
+}
+
 /**
- * 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.
+ * Returns an object that implements DrupalMailSendingInterface.
+ *
+ * Allows for one or more custom mail backends to send mail messages
+ * composed using drupal_mail().
+ *
+ * @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) {
+  static $instances = array();
+
+  $id = $module . '_' . $key;
+  $configuration = variable_get('mail_sending_system', array('default' => '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'];
+  }
+
+  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:
@@ -170,28 +263,7 @@ function drupal_mail($module, $key, $to,
  *   Returns TRUE if the mail was successfully accepted for delivery,
  *   FALSE otherwise.
  */
-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 {
-    $mimeheaders = array();
-    foreach ($message['headers'] as $name => $value) {
-      $mimeheaders[] = $name . ': ' . mime_header_encode($value);
-    }
-    return mail(
-      $message['to'],
-      mime_header_encode($message['subject']),
-      // Note: e-mail uses CRLF for line-endings, but PHP's API requires LF.
-      // They will appear correctly in the actual e-mail that is sent.
-      str_replace("\r", '', $message['body']),
-      // For headers, PHP's API suggests that we use CRLF normally,
-      // but some MTAs incorrecly replace LF with CRLF. See #234403.
-      join("\n", $mimeheaders)
-    );
-  }
+   public function mail($message);
 }
 
 /**
Index: modules/simpletest/tests/mail.test
===================================================================
RCS file: modules/simpletest/tests/mail.test
diff -N modules/simpletest/tests/mail.test
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ modules/simpletest/tests/mail.test	11 Nov 2008 03:57:54 -0000
@@ -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' => '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;
+  }
+}
+
Index: modules/user/user.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/user/user.module,v
retrieving revision 1.932
diff -u -p -r1.932 user.module
--- modules/user/user.module	10 Nov 2008 05:23:01 -0000	1.932
+++ modules/user/user.module	11 Nov 2008 03:57:54 -0000
@@ -2129,7 +2129,7 @@ function user_preferred_language($accoun
  * @param $language
  *  Optional language to use for the notification, overriding account language.
  * @return
- *  The return value from drupal_mail_send(), if ends up being called.
+ *  The return value from drupal_send()->mail(), if it is called.
  */
 function _user_mail_notify($op, $account, $language = NULL) {
   // By default, we always notify except for deleted and blocked.
