--- action_email_role.module.old	2008-06-08 00:05:09.000000000 +1000
+++ action_email_role.module	2008-06-08 03:08:14.000000000 +1000
@@ -38,6 +38,9 @@ function action_email_role_send_email_ac
   if (!isset($context['recipient'])) {
     $context['recipient'] = '';
   }
+  if (!isset($context['condense_recipients'])) {
+    $context['condense_recipients'] = '';
+  }
   if (!isset($context['subject'])) {
     $context['subject'] = '';
   }
@@ -61,6 +64,14 @@ function action_email_role_send_email_ac
     '#required' => TRUE,
   );
 
+  $form['condense_recipients'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Condense Recipients'),
+    '#default_value' => $context['condense_recipients'],
+    '#description' => t('When enabled, this causes all recipients to be visible in the TO field - be aware of privacy implications. Also, only 1 mail call is passed to drupal. Due to this, you will not be notified if there is an issue with any of the individual emails.'),
+    '#required' => FALSE,
+  );
+
   $form['subject'] = array(
     '#type' => 'textfield',
     '#title' => t('Subject'),
@@ -91,6 +102,7 @@ function action_email_role_send_email_ac
   // we return will be serialized to the database.
   $params = array(
     'recipient' => $form_values['recipient'],
+    'condense_recipients' => $form_values['condense_recipients'],
     'subject'   => $form_values['subject'],
     'message'   => $form_values['message'],
   );
@@ -106,6 +118,7 @@ function action_email_role_send_email_ac
 
   $from = variable_get('site_mail', ini_get('sendmail_from'));
   $recipient = $context['recipient'];
+  $condense_recipients = $context['condense_recipients']['condense_recipients'];
   $subject = $context['subject'];
   $message = $context['message'];  
   $site_name = variable_get('site_name', 'DonorFirst Platform');
@@ -125,7 +138,7 @@ function action_email_role_send_email_ac
     $subject = str_replace(array("\r", "\n"), '', $subject);
     $message = strtr($message, $variables);
   }
-  
+
   foreach($recipient as $rid => $rname) {
     if (!empty($rname)) {
       $roles[]=$rid;
@@ -133,14 +146,31 @@ function action_email_role_send_email_ac
   }
 
   $emailed = 0;
+  $recipient_list = array();
   $result = db_query("SELECT ur.*, u.mail, u.name FROM {users_roles} ur LEFT JOIN {users} u ON ur.uid = u.uid WHERE ur.rid IN (%s) AND u.status = 1", implode(',', $roles));
   while($role = db_fetch_object($result)) {
-    if (drupal_mail('action_email_role', $role->mail, $subject, $message, $from)) {
-      watchdog('action_email_role', t('Sent email to %recipient', array('%recipient' => $role->mail)));
+    if ($condense_recipients) {
+      // condensing recipients - don't send mail until we have all recipients
+      $recipient_list[] = $role->mail;
       $emailed++;
     } else {
-      watchdog('error', t('Unable to send email to %recipient from action_email_role', array('%recipient' => $role->mail)));
+      // sending 1 email call per recipient
+      if (drupal_mail('action_email_role', $role->mail, $subject, $message, $from)) {
+        watchdog('action_email_role', t('Sent email to %recipient', array('%recipient' => $role->mail)));
+        $emailed++;
+      } else {
+        watchdog('error', t('Unable to send email to %recipient from action_email_role', array('%recipient' => $role->mail)));
+      }
+    }
+  }
+  if ($condense_recipients) {
+    // time to send single email to drupal with full recipient list in TO field
+    if (drupal_mail('action_email_role', implode(',', $recipient_list) , $subject, $message, $from)) {
+      watchdog('action_email_role', "$emailed users bulk emailed successfully from action_email_role.");
+    } else {
+      watchdog('error', t('Unable to send bulk email from action_email_role'));
     }
+  } else {
+    watchdog('action_email_role',"$emailed users emailed successfully from action_email_role.");
   }
-  watchdog('action_email_role',"$emailed users emailed successfuly from action_email_role.");
 }
