--- mass_contact.module.org	2010-05-12 08:12:45.000000000 +0200
+++ mass_contact.module	2010-06-09 15:46:20.000000000 +0200
@@ -1275,6 +1275,7 @@ function mass_contact_mail_page_submit($
 
   $from_name = ($form_state['values']['name']) ? $form_state['values']['name'] : variable_get('mass_contact_default_sender_name', '');
   $from_email = ($form_state['values']['mail']) ? $form_state['values']['mail'] : variable_get('mass_contact_default_sender_email', '');
+  $from_email = !empty($from_name) ? '"=?UTF-8?Q?'. imap_8bit($from_name). '?=" <'. $from_email .'>' : $from_email;
 
   $boundary_attachment = md5(uniqid(mt_rand()));
   $boundary_html       = md5(uniqid(mt_rand()));
@@ -1494,6 +1495,8 @@ function mass_contact_mail_page_submit($
     $cids[] = $cid;
   }
 
+  global $user;
+  $recipientouta = array();
   $result = db_query("SELECT * FROM {mass_contact} WHERE cid IN(". db_placeholders($cids) .")", $cids);
   while ($contact = db_fetch_object($result)) {
     // Format the subject line.
@@ -1528,11 +1531,7 @@ function mass_contact_mail_page_submit($
       }
     }
 
-
-    global $user;
-
     // Create the recipient list.
-    $recipientouta = array();
     $roles = explode(',', $contact->recipients);
     foreach ($roles as $r) {
       if ($r == 2) { // all users
@@ -1570,74 +1569,31 @@ function mass_contact_mail_page_submit($
         }
       }
     }
+  }
 
-    // Check for empty recipient list.
-    if (count($recipientouta) == 0) {
-      drupal_set_message(t('Either there are no users in the "!category" category, or you are the only user in that category, and therefore, the message was not sent to that category.', array('!category' => $contact->category)));
-      $form_state['redirect'] = '';
-    }
-    else {
-      $recipientout = array();
-      // Check for recipient limit and break up recipient list, if necessary.
-      $recip_limit = variable_get('mass_contact_recipient_limit', 0);
-      if (count($recipientouta) > $recip_limit && $recip_limit != 0) {
-        $countrecip = 0;
-        $ccc = 0;
-        foreach ($recipientouta as $rnamea => $rmaila) {
+  // Check for empty recipient list.
+  if (count($recipientouta) == 0) {
+    drupal_set_message(t('Either there are no users in the "!category" category, or you are the only user in that category, and therefore, the message was not sent to that category.', array('!category' => $contact->category)));
+    $form_state['redirect'] = '';
+  }
+  else {
+    $recipientout = array();
+    // Check for recipient limit and break up recipient list, if necessary.
+    $recip_limit = variable_get('mass_contact_recipient_limit', 0);
+    if (count($recipientouta) > $recip_limit && $recip_limit != 0) {
+      $countrecip = 0;
+      $ccc = 0;
+      foreach ($recipientouta as $rnamea => $rmaila) {
 //          $recipientout[] = $rnamea . " <" . $rmaila . ">";
-          $recipientout[] = $rmaila;
+        $recipientout[] = $rmaila;
 //          $recipient_temp[] = $rnamea . " <" . $rmaila . ">";
-          $recipient_temp[] = $rmaila;
-          $countrecip = count($recipient_temp);
-
-          if ($countrecip == $recip_limit) {
-            $recipient_send = implode(', ', $recipient_temp);
-
-            // set bcc
-            if ($bcc == 1) { // hidden recipients
-              $params['headers']['Bcc'] = $recipient_send;
-              $to = $from_email;
-            }
-            else {
-              $to = $recipient_send;
-            }
-
-            ++$ccc;
-
-            // Send the e-mail to the recipients:
-/*
-            if (module_exists('mimemail') && variable_get('mimemail_alter', 0) == 1) {
-              $body = implode("\n\n", $params['body']);
-              if ($form_state['values']['html'] == 1) {
-                $success = mimemail($from_email, $to, $params['subject'], $body, NULL, $params['headers'], NULL, $message_attachment);
-              }
-              else {
-                $success = mimemail($from_email, $to, $params['subject'], $body, TRUE, $params['headers'], $body, $message_attachment);
-              }
-            }
-            else {
-*/
-              // This call was changed from drupal_mail() due to this bug report:
-              // http://drupal.org/node/473838
-              $success = _mass_contact_mail('mass_contact', 'mail_page', $to, language_default(), $params, $from_email);
-//            }
+        $recipient_temp[] = $rmaila;
+        $countrecip = count($recipient_temp);
 
-            if ($success['result']) {
-              drupal_set_message(t('[Success] Send #!ccc: -e-mails', array('!ccc' => $ccc, '-e-mails' => $recipient_send)));
-            }
-            else {
-              ++$send_error;
-            }
-
-            // reset array
-            $recipient_temp = array();
-            $countrecip = 0;
-          }
-        }
-
-        // Send the remainder.
-        if ($countrecip != 0) {
+        if ($countrecip == $recip_limit) {
           $recipient_send = implode(', ', $recipient_temp);
+
+          // set bcc
           if ($bcc == 1) { // hidden recipients
             $params['headers']['Bcc'] = $recipient_send;
             $to = $from_email;
@@ -1664,36 +1620,35 @@ function mass_contact_mail_page_submit($
             // This call was changed from drupal_mail() due to this bug report:
             // http://drupal.org/node/473838
             $success = _mass_contact_mail('mass_contact', 'mail_page', $to, language_default(), $params, $from_email);
-//          }
+//            }
 
           if ($success['result']) {
-            drupal_set_message(t('[Success] Send Remainder: -e-mails', array('-e-mails' => $recipient_send)));
+            drupal_set_message(t('[Success] Send #!ccc: -e-mails', array('!ccc' => $ccc, '-e-mails' => $recipient_send)));
           }
           else {
             ++$send_error;
           }
+
+          // reset array
+          $recipient_temp = array();
+          $countrecip = 0;
         }
-        $total_recip = count($recipientout);
-        $recipientout = implode(', ', $recipientout);
       }
-      else {
-        foreach ($recipientouta as $rnamea => $rmaila) {
-//          $recipientout[] = $rnamea . " <" . $rmaila . ">";
-          $recipientout[] = $rmaila;
-        }
-        $total_recip = count($recipientout);
-        $recipientout = implode(', ', $recipientout);
 
-        // set bcc
+      // Send the remainder.
+      if ($countrecip != 0) {
+        $recipient_send = implode(', ', $recipient_temp);
         if ($bcc == 1) { // hidden recipients
-          $params['headers']['Bcc'] = $recipientout;
+          $params['headers']['Bcc'] = $recipient_send;
           $to = $from_email;
         }
         else {
-          $to = $recipientout;
+          $to = $recipient_send;
         }
 
-        // Send the e-mail to the recipients.
+        ++$ccc;
+
+        // Send the e-mail to the recipients:
 /*
         if (module_exists('mimemail') && variable_get('mimemail_alter', 0) == 1) {
           $body = implode("\n\n", $params['body']);
@@ -1709,90 +1664,134 @@ function mass_contact_mail_page_submit($
           // This call was changed from drupal_mail() due to this bug report:
           // http://drupal.org/node/473838
           $success = _mass_contact_mail('mass_contact', 'mail_page', $to, language_default(), $params, $from_email);
-//        }
+//          }
 
         if ($success['result']) {
-          drupal_set_message(t('[Success] Send Once: -e-mails', array('-e-mails' => $recipientout)));
+          drupal_set_message(t('[Success] Send Remainder: -e-mails', array('-e-mails' => $recipient_send)));
         }
         else {
           ++$send_error;
         }
       }
+      $total_recip = count($recipientout);
+      $recipientout = implode(', ', $recipientout);
     }
+    else {
+      foreach ($recipientouta as $rnamea => $rmaila) {
+//          $recipientout[] = $rnamea . " <" . $rmaila . ">";
+        $recipientout[] = $rmaila;
+      }
+      $total_recip = count($recipientout);
+      $recipientout = implode(', ', $recipientout);
 
-    // Send a copy to self, if requested.
-    if ($form_state['values']['copy'] && !$bcc) {
-      // This call was changed from drupal_mail() due to this bug report:
-      // http://drupal.org/node/473838
-      $copy_results = _mass_contact_mail('mass_contact', 'user-copy', $from_email, language_default(), $params, $from_email);
-      // Inform the user.
-      if ($copy_results['result']) {
-        drupal_set_message(t('A carbon copy was sent to you.'));
+      // set bcc
+      if ($bcc == 1) { // hidden recipients
+        $params['headers']['Bcc'] = $recipientout;
+        $to = $from_email;
       }
       else {
-        if (user_access('access site reports')) {
-          drupal_set_message(t('A carbon copy could not be sent to you. Please check the !logs for errors.', array('!logs' => l('logs', 'admin/reports/dblog'))));
+        $to = $recipientout;
+      }
+
+      // Send the e-mail to the recipients.
+/*
+      if (module_exists('mimemail') && variable_get('mimemail_alter', 0) == 1) {
+        $body = implode("\n\n", $params['body']);
+        if ($form_state['values']['html'] == 1) {
+          $success = mimemail($from_email, $to, $params['subject'], $body, NULL, $params['headers'], NULL, $message_attachment);
         }
         else {
-          drupal_set_message(t('A carbon copy could not be sent to you. Please have a system administrator check the logs for errors.'));
+          $success = mimemail($from_email, $to, $params['subject'], $body, TRUE, $params['headers'], $body, $message_attachment);
         }
       }
-    }
-
-    // Error checking & reporting and node saving.
-    // Check for errors.
-    if ($send_error == 0) {
-      // If there are no errors, check to see if we need to save the message as a node.
-      if ($nodecc == 1) {
-        // Check to see if the node type exists.
-        $check = db_fetch_array(db_query("SELECT * FROM {node_type} WHERE type = 'mass_contact'"));
-        if (!$check) {
-          // If the node type does not exist, create it.
-          _mass_contact_create_node_type();
-        }
+      else {
+*/
+        // This call was changed from drupal_mail() due to this bug report:
+        // http://drupal.org/node/473838
+        $success = _mass_contact_mail('mass_contact', 'mail_page', $to, language_default(), $params, $from_email);
+//        }
 
-        // Add the attachment, if it exists.
-        if ($_FILES['files']['size']['attachment'] > 0) {
-          $file_data  = fread(fopen($_FILES['files']['tmp_name']['attachment'], 'r'), filesize($_FILES['files']['tmp_name']['attachment']));
+      if ($success['result']) {
+        drupal_set_message(t('[Success] Send Once: -e-mails', array('-e-mails' => $recipientout)));
+      }
+      else {
+        ++$send_error;
+      }
+    }
+  }
 
-          $file_dest  = variable_get('mass_contact_attachment_location', file_directory_path() .'/mass_contact_attachments');
-          file_check_directory($file_dest, FILE_CREATE_DIRECTORY);
+  // Send a copy to self, if requested.
+  if ($form_state['values']['copy'] && !$bcc) {
+    // This call was changed from drupal_mail() due to this bug report:
+    // http://drupal.org/node/473838
+    $copy_results = _mass_contact_mail('mass_contact', 'user-copy', $from_email, language_default(), $params, $from_email);
+    // Inform the user.
+    if ($copy_results['result']) {
+      drupal_set_message(t('A carbon copy was sent to you.'));
+    }
+    else {
+      if (user_access('access site reports')) {
+        drupal_set_message(t('A carbon copy could not be sent to you. Please check the !logs for errors.', array('!logs' => l('logs', 'admin/reports/dblog'))));
+      }
+      else {
+        drupal_set_message(t('A carbon copy could not be sent to you. Please have a system administrator check the logs for errors.'));
+      }
+    }
+  }
 
-          $file_path  = file_save_data($file_data, $file_dest .'/'. $_FILES['files']['name']['attachment'], FILE_EXISTS_RENAME);
-          $file_url   = file_create_url($file_path);
+  // Error checking & reporting and node saving.
+  // Check for errors.
+  if ($send_error == 0) {
+    // If there are no errors, check to see if we need to save the message as a node.
+    if ($nodecc == 1) {
+      // Check to see if the node type exists.
+      $check = db_fetch_array(db_query("SELECT * FROM {node_type} WHERE type = 'mass_contact'"));
+      if (!$check) {
+        // If the node type does not exist, create it.
+        _mass_contact_create_node_type();
+      }
 
-          $node_message = '<hr>Attachment:<br />'. l($_FILES['files']['name']['attachment'], $file_url);
-        }
+      // Add the attachment, if it exists.
+      if ($_FILES['files']['size']['attachment'] > 0) {
+        $file_data  = fread(fopen($_FILES['files']['tmp_name']['attachment'], 'r'), filesize($_FILES['files']['tmp_name']['attachment']));
 
-        // Save the message as a node.
-        _mass_contact_save_node($params['subject'], $node_message, $recipientout, $contact->category, $roles, $user->uid);
-        // Get new node id.
-        $nidobj = db_fetch_object(db_query("SELECT nid FROM {node} WHERE type = 'mass_contact' ORDER BY changed DESC"));
-        $nid = $nidobj->nid;
+        $file_dest  = variable_get('mass_contact_attachment_location', file_directory_path() .'/mass_contact_attachments');
+        file_check_directory($file_dest, FILE_CREATE_DIRECTORY);
 
-        // Log node creation.
-        watchdog('content', 'Mass Contact content added "%title".', array('%title' => $params['subject']), WATCHDOG_NOTICE, l(t('View.'), "node/$nid"));
+        $file_path  = file_save_data($file_data, $file_dest .'/'. $_FILES['files']['name']['attachment'], FILE_EXISTS_RENAME);
+        $file_url   = file_create_url($file_path);
 
-        // Inform the user.
-        drupal_set_message(t('A carbon copy has been created as a node.'));
+        $node_message = '<hr>Attachment:<br />'. l($_FILES['files']['name']['attachment'], $file_url);
       }
 
-      // Log the operation.
-      flood_register_event('mass_contact');
-      watchdog('mass_contact', '%name-from sent an e-mail to the %category group.', array('%name-from' => $from_name .' <'. $from_email .'>', '%category' => $contact->category));
+      // Save the message as a node.
+      _mass_contact_save_node($params['subject'], $node_message, $recipientout, $contact->category, $roles, $user->uid);
+      // Get new node id.
+      $nidobj = db_fetch_object(db_query("SELECT nid FROM {node} WHERE type = 'mass_contact' ORDER BY changed DESC"));
+      $nid = $nidobj->nid;
+
+      // Log node creation.
+      watchdog('content', 'Mass Contact content added "%title".', array('%title' => $params['subject']), WATCHDOG_NOTICE, l(t('View.'), "node/$nid"));
 
       // Inform the user.
-      if (count($recipientouta) > 0) {
-        drupal_set_message(t('Message sent successfully to !total users: -e-mails', array('!total' => $total_recip, '-e-mails' => $recipientout)));
-      }
+      drupal_set_message(t('A carbon copy has been created as a node.'));
+    }
+
+    // Log the operation.
+    flood_register_event('mass_contact');
+    watchdog('mass_contact', '%name-from sent an e-mail to the %category group.', array('%name-from' => $from_name .' <'. $from_email .'>', '%category' => $contact->category));
+
+    // Inform the user.
+    if (count($recipientouta) > 0) {
+      drupal_set_message(t('Message sent successfully to !total users: -e-mails', array('!total' => $total_recip, '-e-mails' => $recipientout)));
+    }
+  }
+  else {
+    if (user_access('access site reports')) {
+      drupal_set_message(t('!errors error(s) were encountered sending the message. Please check the !logs for errors.', array('!errors' => $send_error, '!logs' => l('logs', 'admin/reports/dblog'))));
     }
     else {
-      if (user_access('access site reports')) {
-        drupal_set_message(t('!errors error(s) were encountered sending the message. Please check the !logs for errors.', array('!errors' => $send_error, '!logs' => l('logs', 'admin/reports/dblog'))));
-      }
-      else {
-        drupal_set_message(t('!errors error(s) were encountered sending the message. Please have a system administrator check the logs for errors.', array('!errors' => $send_error)));
-      }
+      drupal_set_message(t('!errors error(s) were encountered sending the message. Please have a system administrator check the logs for errors.', array('!errors' => $send_error)));
     }
   }
 
