From d7b22b97b0e729291365e6b3d7d902ed7aa0f4e9 Mon Sep 17 00:00:00 2001
From: Bob Vincent <bobvin@pillars.net>
Date: Fri, 22 Apr 2011 11:47:11 -0400
Subject: [PATCH] Issue #1135262 by pillarsdotnet: Support using different MailSystemInterface classes for format() and mail().

---
 includes/mail.inc |   81 +++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 63 insertions(+), 18 deletions(-)

diff --git a/includes/mail.inc b/includes/mail.inc
index d2febed39686c9bf3f6f7a2bf99fa1377d09f4de..c797bda31fe94dba80560f27588d9a3ec7d06639 100644
--- a/includes/mail.inc
+++ b/includes/mail.inc
@@ -141,14 +141,15 @@ function drupal_mail($module, $key, $to, $language, $params = array(), $from = N
   drupal_alter('mail', $message);
 
   // Retrieve the responsible implementation for this message.
-  $system = drupal_mail_system($module, $key);
+  $formatter = drupal_mail_system($module, $key, 'format');
 
   // Format the message body.
-  $message = $system->format($message);
+  $message = $formatter->format($message);
 
   // Optionally send e-mail.
   if ($send) {
-    $message['result'] = $system->mail($message);
+    $mailer = drupal_mail_system($module, $key, 'mail');
+    $message['result'] = $mailer->mail($message);
 
     // Log errors
     if (!$message['result']) {
@@ -161,7 +162,7 @@ function drupal_mail($module, $key, $to, $language, $params = array(), $from = N
 }
 
 /**
- * Returns an object that implements the MailSystemInterface.
+ * Returns an object that implements a MailSystemInterface method.
  *
  * Allows for one or more custom mail backends to format and send mail messages
  * composed using drupal_mail().
@@ -182,14 +183,27 @@ function drupal_mail($module, $key, $to, $language, $params = array(), $from = N
  * 'mail_system', which is a keyed array.  The default implementation
  * is the class whose name is the value of 'default-system' key. A more specific
  * match first to key and then to module will be used in preference to the
- * default. To specificy a different class for all mail sent by one module, set
+ * default. To specify a different class for all mail sent by one module, set
  * the class name as the value for the key corresponding to the module name. To
- * specificy a class for a particular message sent by one module, set the class
+ * specify a class for a particular message sent by one module, set the class
  * name as the value for the array key that is the message id, which is
  * "${module}_${key}".
  *
- * 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:
+ * To specify that the "FooMailSystem" class should be used for the format()
+ * method and the "BarMailSystem" class should be used for the mail() method,
+ * set the variable as follows:
+ *
+ * @code
+ * array(
+ *   'default-system' => array(
+ *     'format' => 'FooMailsystem',
+ *     'mail' => 'BarMailSystem',
+ *   ),
+ * );
+ * @endcode
+ *
+ * To debug all mail sent by the user module by logging it to a file, you might
+ * set the variable as something like:
  *
  * @code
  * array(
@@ -220,10 +234,13 @@ function drupal_mail($module, $key, $to, $language, $params = array(), $from = N
  * @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}.
+ * @param $method
+ *   (optional) A MailSystemInterface method.  If set, must be either 'format'
+ *   or 'mail'.  Defaults to 'format'.
  *
  * @return MailSystemInterface
  */
-function drupal_mail_system($module, $key) {
+function drupal_mail_system($module, $key, $method = 'format') {
   $instances = &drupal_static(__FUNCTION__, array());
 
   $id = $module . '_' . $key;
@@ -232,15 +249,43 @@ function drupal_mail_system($module, $key) {
 
   // 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'];
-  }
+  do {
+    if (isset($configuration[$id])) {
+      if (is_array($configuration[$id])) {
+        if (isset($configuration[$id][$method])) {
+          $class = $configuration[$id][$method];
+          break;
+        }
+      }
+      else {
+        $class = $configuration[$id];
+        break;
+      }
+    }
+    if (isset($configuration[$module])) {
+      if (is_array($configuration[$module])) {
+        if (isset($configuration[$module][$method])) {
+          $class = $configuration[$module][$method];
+          break;
+        }
+      }
+      else {
+        $class = $configuration[$module];
+        break;
+      }
+    }
+    if (is_array($configuration['default-system'])) {
+      if (isset($configuration['default-system'][$method])) {
+        $class = $configuration['default-system'][$method];
+      }
+      else {
+        $class = 'DefaultMailSystem';
+      }
+    }
+    else {
+      $class = $configuration['default-system'];
+    }
+  } while (FALSE);
 
   if (empty($instances[$class])) {
     $interfaces = class_implements($class);
-- 
1.7.1

