Index: simplenews/simplenews.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/simplenews/simplenews.module,v
retrieving revision 1.135
diff -u -p -r1.135 simplenews.module
--- simplenews/simplenews.module	4 Aug 2008 06:39:32 -0000	1.135
+++ simplenews/simplenews.module	4 Aug 2008 15:08:12 -0000
@@ -904,7 +904,7 @@ function simplenews_subscribe_user($mail
     $params['from'] = _simplenews_set_from();
     $params['newsletter'] = taxonomy_get_term($tid);
     $params['context']['account'] = $subscription;
-    drupal_mail('simplenews', 'subscribe', $mail, $subscription->language, $params, $params['from']['address']);
+    simplenews_backend_mail('simplenews', 'subscribe', $mail, $subscription->language, $params, $params['from']['address']);
   }
   elseif (!isset($subscription->tids[$tid])) {
     // OR add user to newsletter relationship if not already subscribed.
@@ -948,7 +948,7 @@ function simplenews_unsubscribe_user($ma
     $params['from'] = _simplenews_set_from();
     $params['newsletter'] = $newsletter;
     $params['context']['account'] = $subscription;
-    drupal_mail('simplenews', 'unsubscribe', $mail, $subscription->language, $params, $params['from']['address']);
+    simplenews_backend_mail('simplenews', 'unsubscribe', $mail, $subscription->language, $params, $params['from']['address']);
   }
   elseif (isset($subscription->tids[$tid])) {
     // OR remove the user from the newsletter.
@@ -1302,11 +1302,11 @@ function simplenews_send_test($node, $ac
       }
     }
 
-    // Test emails are send directly by drupal_mail(). This in contrast to simplenews_send_node() which send via a buffer.
+    // Test emails are send directly by simplenews_backend_mail(). This in contrast to simplenews_send_node() which send via a buffer.
     foreach ($accounts as $account) {
       $subscription = simplenews_get_subscription($account);
       $params['context']['account'] = $subscription;
-      $result = drupal_mail('simplenews', 'test', $account->mail, $subscription->language, $params, $from['address']);
+      $result = simplenews_backend_mail('simplenews', 'test', $account->mail, $subscription->language, $params, $from['address']);
       if ($result['result']) {
         drupal_set_message(t('Test newsletter sent to %recipient.', array('%recipient' => $account->mail)));
       }
@@ -1426,22 +1426,25 @@ function simplenews_mail($key, &$message
         // Buffer body text node and language specific
         $messages[$nid][$langcode]['body'] = $body;
 
+        // Buffer newsletter format.
+        // Newsletter format is required for footer generation and
+        // by simplenews_backend_mail() to send cached emails.
+        $message['format'] = $messages[$nid][$langcode]['format'] = $context['node']->simplenews['s_format'];
       }
       else {
         // Get message data from buffer
         $message['headers'] = $messages[$nid][$langcode]['headers'];
         $message['subject'] = $messages[$nid][$langcode]['subject'];
+        $message['format'] = $messages[$nid][$langcode]['format'];
         $body = $messages[$nid][$langcode]['body'];
       }
 
       // Build message body.
       // The placeholders are replaced with user specific data
       // before conversion to plain text.
-      $variables =  user_mail_tokens($context['account'], $context['account']->language);
+      $variables = user_mail_tokens($context['account'], $context['account']->language);
       $body = strtr($body, $variables);
-      if ($context['node']->simplenews['s_format'] == 'plain') {
-        $body = simplenews_html_to_text($body);
-      }
+
       $message['body']['body'] = $body;
 
       // Build message footer.
@@ -1451,7 +1454,7 @@ function simplenews_mail($key, &$message
       else {
         $hash = '';
       }
-      $message['body']['footer'] = theme('simplenews_newsletter_footer', $context['node']->simplenews['s_format'], $hash, $key == 'test', $message['language']);
+      $message['body']['footer'] = theme('simplenews_newsletter_footer', $message['format'], $hash, $key == 'test', $message['language']);
 
       // Add user specific header data.
       $message['headers']['List-Unsubscribe'] = '<'. url('newsletter/confirm/remove/'. $hash, array('absolute' => TRUE)) .'>';
@@ -1544,7 +1547,7 @@ function simplenews_mail_send($nid = NUL
       $params['context']['node'] = $node;
 
       // Send mail
-      $message = drupal_mail('simplenews', 'node', $subscription->mail, $subscription->language, $params, $params['from']['address'], TRUE);
+      $message = simplenews_backend_mail('simplenews', 'node', $subscription->mail, $subscription->language, $params, $params['from']['address']);
 
       if (variable_get('simplenews_debug', FALSE)) {
         watchdog('simplenews', 'Outgoing email. Message type: %type<br />Subject: %subject<br />Recipient: %to', array('%type' => 'node', '%to' => $message['to'], '%subject' => $message['subject']), WATCHDOG_DEBUG);
@@ -1565,6 +1568,36 @@ function simplenews_mail_send($nid = NUL
 }
 
 /**
+ * Wrapper for drupal_mail to send a message through the correct backend. All arguments are the same as drupal_mail().
+ *
+ * Two backends are available: Drupal mail and mimemail module.
+ * Backend selection depend on presence of mimemail module.
+ * Mime mail module can send both plain and html e-mails depending on
+ * newsletter format selection.
+ */
+function simplenews_backend_mail($module, $key, $to, $language, $params = array(), $from = NULL, $send = TRUE) {
+  $message = drupal_mail($module, $key, $to, $language, $params, $from, FALSE);
+
+  if ($send) {
+    if (module_exists('mimemail')) {
+      // Newsletters with format 'plain' will be send by mimemail as plain text.
+      // This requires $plaintext = TRUE and $text to contain the plain text body.
+      $plaintext = $message['format'] == 'plain';
+      $text = $plaintext ? simplenews_html_to_text($message['body']) : NULL;
+
+      $sent = mimemail($message['from'], $message['to'], $message['subject'], $message['body'], $plaintext, $message['headers'], $text, array());
+    }
+    else {
+      $sent = drupal_mail_send($message);
+    }
+
+    $message['result'] = $sent;
+  }
+
+  return $message;  
+}
+
+/**
  * Store mail message in mail cache table.
  *
  * @param array $message data array to be stored
