commit d1553433bb33af43fdf2e9caab9ca8b44628f0a0
Author: Sascha Seidel <seidels@informatik.uni-luebeck.de>
Date:   Sat Jan 14 19:56:08 2012 +0100

    Fixed sending of subscription emails.

diff --git a/includes/simplenews.mail.inc b/includes/simplenews.mail.inc
index a70525a..cc50a31 100644
--- a/includes/simplenews.mail.inc
+++ b/includes/simplenews.mail.inc
@@ -185,33 +185,7 @@ function simplenews_mail_mail($nid, $vid, $mail, $key = 'node') {
     $params['context']['node'] = $node;
 
     // Send mail
-    if (module_exists('mimemail')) {
-      // If mimemail module is installed ALL emails are send via this module.
-      // drupal_mail() builds the content of the email but does NOT send.
-      $message = drupal_mail('simplenews', $key, $subscription->mail, $subscription->language, $params, $params['from']['formatted'], FALSE);
-      $to = isset($message['params']['context']['account']) ? $message['params']['context']['account'] : $message['to'];
-      $plain = $message['params']['context']['node']->simplenews['s_format'] == 'plain' ? TRUE : NULL;
-      $mimemail_result = mimemail(
-        $message['from'],
-        $to,
-        $message['subject'],
-        $message['body'],
-        $plain,
-        $message['headers'],
-        $plain ? $message['body'] : simplenews_html_to_text($message['body'], TRUE),
-        isset($message['params']['context']['node']->files) ? $message['params']['context']['node']->files : array(),
-        $message['id']
-      );
-      // Mimemail has changed its API (see http://drupal.org/node/808518) but we keep backward compatibility
-      if (is_array($mimemail_result)) {
-        $message = $mimemail_result;
-      } else {
-        $message['result'] = $mimemail_result;
-      }
-    }
-    else {
-      $message = drupal_mail('simplenews', $key, $subscription->mail, $subscription->language, $params, $params['from']['formatted'], TRUE);
-    }
+    $message = simplenews_mail_wrapper($key, $subscription, $params);
 
     // Log sent result in watchdog.
     if (variable_get('simplenews_debug', FALSE)) {
@@ -261,6 +235,50 @@ function simplenews_mail_mail($nid, $vid, $mail, $key = 'node') {
 }
 
 /**
+ * Prepare an email and send it either with mimemail() or drupal_mail().
+ *
+ * @param $key email key [node|subscribe|unsubscribe]
+ * @param $subscription subscription data for recipient and language
+ * @param $params array with email parameters
+ *
+ * @return TRUE if email is successfully delivered
+ */
+function simplenews_mail_wrapper($key, $subscription, $params) {
+  $message = array();
+
+  // Send mail
+  if (module_exists('mimemail')) {
+    // If mimemail module is installed ALL emails are send via this module.
+    // drupal_mail() builds the content of the email but does NOT send.
+    $message = drupal_mail('simplenews', $key, $subscription->mail, $subscription->language, $params, $params['from']['formatted'], FALSE);
+    $to = isset($message['params']['context']['account']) ? $message['params']['context']['account'] : $message['to'];
+    $plain = $message['params']['context']['node']->simplenews['s_format'] == 'plain' ? TRUE : NULL;
+    $mimemail_result = mimemail(
+      $message['from'],
+      $to,
+      $message['subject'],
+      $message['body'],
+      $plain,
+      $message['headers'],
+      $plain ? $message['body'] : simplenews_html_to_text($message['body'], TRUE),
+      isset($message['params']['context']['node']->files) ? $message['params']['context']['node']->files : array(),
+      $message['id']
+    );
+    // Mimemail has changed its API (see http://drupal.org/node/808518) but we keep backward compatibility
+    if (is_array($mimemail_result)) {
+      $message = $mimemail_result;
+    } else {
+      $message['result'] = $mimemail_result;
+    }
+  }
+  else {
+    $message = drupal_mail('simplenews', $key, $subscription->mail, $subscription->language, $params, $params['from']['formatted'], TRUE);
+  }
+
+  return $message;
+}
+
+/**
  * Send simplenews newsletters from the spool.
  *
  * Individual newsletter emails are stored in database spool.
diff --git a/simplenews.module b/simplenews.module
index 506c2a0..3c663ab 100644
--- a/simplenews.module
+++ b/simplenews.module
@@ -409,7 +409,7 @@ function simplenews_node_tab_send_form($form_state, $node) {
         '#value' => key($format_options),
       );
     }
-    
+
     // include function that provides the priority values
     module_load_include('inc', 'simplenews', 'includes/simplenews.admin');
 
@@ -427,13 +427,13 @@ function simplenews_node_tab_send_form($form_state, $node) {
     );
   }
   else {
-   
+
     $form['simplenews']['none'] = array(
       '#type' => 'checkbox',
       '#return_value' => 0,
       '#attributes' => array('checked' => 'checked', 'disabled' => 'disabled'),
     );
-    
+
     $form['simplenews']['none']['#title'] = ($simplenews_values['s_status'] == SIMPLENEWS_STATUS_SEND_READY) ? t('This newsletter has been sent.') :  t('This newsletter is pending.');
     // We return now, because there's no point in having a submit button
     return $form;
@@ -481,13 +481,13 @@ function simplenews_node_tab_send_form_submit($form, &$form_state) {
   // Get the node
   $values = $form_state['values'];
   $node = node_load($values['nid']);
-  
+
   $s_status = SIMPLENEWS_STATUS_SEND_NOT;
   if ($values['simplenews']['send'] == SIMPLENEWS_COMMAND_SEND_NOW) {
     $s_status = SIMPLENEWS_STATUS_SEND_PENDING;
   }
   $values['simplenews']['s_status'] = $s_status;
-  
+
   // Is this a multilingual newsletter ?
   // When this node is selected for translation all translation of this node
   // will be sent too.
@@ -1442,7 +1442,9 @@ function simplenews_subscribe_user($mail, $tid, $confirm = TRUE, $source = 'unkn
     $params['from'] = _simplenews_set_from();
     $params['context']['newsletter'] = taxonomy_get_term($tid);
     $params['context']['account'] = $subscription;
-    drupal_mail('simplenews', 'subscribe', $mail, $subscription->language, $params, $params['from']['address']);
+
+    // Send mail
+    $message = simplenews_mail_wrapper('subscribe', $subscription, $params);
   }
   elseif (!isset($subscription->tids[$tid])) {
     // Add user to newsletter relationship if not already subscribed.
@@ -1519,7 +1521,9 @@ function simplenews_unsubscribe_user($mail, $tid, $confirm = TRUE, $source = 'un
     $params['from'] = _simplenews_set_from();
     $params['context']['newsletter'] = $newsletter;
     $params['context']['account'] = $subscription;
-    drupal_mail('simplenews', 'unsubscribe', $mail, $subscription->language, $params, $params['from']['address']);
+
+    // Send mail
+    $message = simplenews_mail_wrapper('unsubscribe', $subscription, $params);
   }
   elseif (isset($subscription->tids[$tid])) {
     // Unsubscribe the user from the newsletter.
@@ -1810,7 +1814,7 @@ function simplenews_mail($key, &$message, $params) {
         $subject = token_replace($subject, 'node', $context['node']);
         $subject = str_replace(array("\r", "\n"), '', $subject);
         $message['subject'] = $messages[$nid][$langcode]['subject'] = $subject;
-        
+
         // Set the active language to the node's language.
         // This needs to be done as otherwise the language used to send the mail
         // is the language of the user logged in.
@@ -1844,7 +1848,7 @@ function simplenews_mail($key, &$message, $params) {
             'simplenews_newsletter_footer'), $context, $key, $message['language']);
           $messages[$nid][$langcode]['footer'] = $footer;
         }
-        
+
         // Reset the language to the original settings.
         if (module_exists('i18n')) {
           i18n_selection_mode('reset');
