You are here

mass_contact.module in Mass Contact 5

Enables mass contact form to selected roles.

File

mass_contact.module
View source
<?php

/**
 * @file
 * Enables mass contact form to selected roles.
 */

/**
 * Implementation of hook_help().
 */
function mass_contact_help($section) {
  switch ($section) {
    case 'admin/help#mass_contact':
      $output .= '<p><b>' . t('Related tasks:') . '</b><br/>' . t('1. <a href="/admin/build/mass_contact/add">Add a category</a><br/>2. <a href="/admin/build/mass_contact">View categories</a><br/>3. <a href="/admin/build/mass_contact/settings">Configure the module</a><br/>4. <a href="/admin/user/access#module-mass_contact">Set Permissions</a><br />5. <a href="/mass_contact">Send mass e-mail</a>') . '</p>';
      $output .= '<p>' . t('The Mass Contact module is simply a modified version of the core contact module.	It works opposite the latter, in that it allows site moderators (or anyone with permission), to <a href="/mass_contact">send mass e-mail</a> to a set role or group of roles or even to all registered users.') . '</p>';
      $output .= '<p>' . t("The sender's own address may be placed in the 'To:' field and all recipients placed in the 'Bcc:' field, or the recipients simply placed in the 'To:' field.\tNote that the latter option leaves all recipients open to abuse due to their e-mail addresses being visible to all other recipients.") . '</p>';
      $output .= '<p>' . t("The e-mail may be sent as html or plain text, and may include a single binary file attachment (if permitted by admin).") . '</p>';
      $output .= '<p>' . t("At the option of the sender (if permitted by admin), a node may be created in order to keep a record of the e-mail sent.  Do not try to send e-mails by creating nodes; it will not work.") . '</p>';
      $output .= '<p>' . t('Users may opt-out of mass mailings on their profile page, but this may be overridden by the admin (or respected).  The entire opt-out system may be disabled on the <a href="@settings-page">settings page</a>.', array(
        '@settings-page' => url('admin/build/mass_contact/settings'),
      )) . '</p>';
      $output .= '<p>' . t('Make sure to add at least one category and configure the module before trying to send mass e-mails.') . '</p>';
      if (!module_exists('menu')) {
        $menu_note = t('The menu item can be customized and configured only once the menu module has been <a href="@modules-page">enabled</a>.', array(
          '@modules-page' => url('admin/\\/modules'),
        ));
      }
      else {
        $menu_note = '';
      }
      $output .= '<p>' . t('The Mass Contact module also adds a <a href="@menu-settings">menu item</a> (disabled by default) to the navigation block.', array(
        '@menu-settings' => url('admin/build/menu'),
      )) . ' ' . $menu_note . '</p>';
      return $output;
  }
}

/**
 * Implementation of hook_perm
 */
function mass_contact_perm() {
  return array(
    'access mass contact form',
    'choose whether to archive mass contact messages',
    'send mass contact attachments',
  );
}

/**
 * Implementation of hook_menu().
 */
function mass_contact_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/build/mass_contact',
      'title' => t('Mass Contact form'),
      'description' => t('Create a mass contact form and set up categories for the form to use.'),
      'callback' => 'mass_contact_admin_categories',
      'access' => user_access('administer site configuration'),
    );
    $items[] = array(
      'path' => 'admin/build/mass_contact/list',
      'title' => t('List'),
      'callback' => 'mass_contact_admin_categories',
      'access' => user_access('administer site configuration'),
      'type' => MENU_DEFAULT_LOCAL_TASK,
    );
    $items[] = array(
      'path' => 'admin/build/mass_contact/add',
      'title' => t('Add category'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'mass_contact_admin_edit',
      ),
      'access' => user_access('administer site configuration'),
      'type' => MENU_LOCAL_TASK,
      'weight' => 1,
    );
    $items[] = array(
      'path' => 'admin/build/mass_contact/edit',
      'title' => t('Edit Mass Contact category'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'mass_contact_admin_edit',
      ),
      'access' => user_access('administer site configuration'),
      'type' => MENU_CALLBACK,
    );
    $items[] = array(
      'path' => 'admin/build/mass_contact/delete',
      'title' => t('Delete Mass Contact category'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'mass_contact_admin_delete',
      ),
      'access' => user_access('administer site configuration'),
      'type' => MENU_CALLBACK,
    );
    $items[] = array(
      'path' => 'admin/build/mass_contact/settings',
      'title' => t('Settings'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'mass_contact_admin_settings',
      ),
      'access' => user_access('administer site configuration'),
      'type' => MENU_LOCAL_TASK,
      'weight' => 2,
    );
    $items[] = array(
      'path' => 'mass_contact',
      'title' => t('Mass Contact'),
      'callback' => 'mass_contact_site_page',
      'access' => user_access('access mass contact form'),
      'type' => MENU_SUGGESTED_ITEM,
    );
    $items[] = array(
      'path' => 'node/add/mass_contact',
      'title' => t('Mass Contact'),
      'access' => user_access('create mass_contact content'),
      'type' => MENU_SUGGESTED_ITEM,
    );
  }
  return $items;
}

/**
 * Categories/list tab.
 */
function mass_contact_admin_categories() {
  $result = db_query('SELECT cid, category, recipients, selected FROM {mass_contact}');
  $rows = array();
  while ($category = db_fetch_object($result)) {
    $rolenamesa = array();
    foreach (explode(',', $category->recipients) as $rid) {
      $namerole = db_fetch_object(db_query('SELECT name FROM {role} WHERE rid = %d', $rid));
      $rolenamesa[] = $namerole->name;
    }
    $rolenames = implode(', ', $rolenamesa);
    $rows[] = array(
      $category->category,
      $rolenames,
      $category->selected ? t('Yes') : t('No'),
      l(t('edit'), 'admin/build/mass_contact/edit/' . $category->cid),
      l(t('delete'), 'admin/build/mass_contact/delete/' . $category->cid),
    );
  }
  $header = array(
    t('Category'),
    t('Recipients'),
    t('Selected'),
    array(
      'data' => t('Operations'),
      'colspan' => 2,
    ),
  );
  return theme('table', $header, $rows);
}

/**
 * Category edit page.
 */
function mass_contact_admin_edit($cid = NULL) {
  if (arg(3) == "edit" && $cid > 0) {
    $edit = db_fetch_array(db_query("SELECT * FROM {mass_contact} WHERE cid = %d", $cid));
  }
  $form['category'] = array(
    '#type' => 'textfield',
    '#title' => t('Category'),
    '#maxlength' => 255,
    '#default_value' => $edit['category'],
    '#description' => t("Will appear in the subject of your email as [category]."),
    '#required' => TRUE,
  );

  // get all roles except anonymous
  $allroles = db_query('SELECT rid, name FROM {role} WHERE rid > 1');
  while ($roleobj = db_fetch_object($allroles)) {
    $onerid = $roleobj->rid;
    $onename = $roleobj->name;
    $rolesarray[$onerid] = $onename;
  }
  $form['recipients'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Roles to receive email'),
    '#options' => $rolesarray,
    '#default_value' => explode(',', $edit['recipients']),
    '#description' => t('These roles will be added to the mailing list.	Note: if you check "authenticated users", other roles will not be added, as they will receive the email anyway.'),
  );
  $form['selected'] = array(
    '#type' => 'select',
    '#title' => t('Selected'),
    '#options' => array(
      '0' => t('No'),
      '1' => t('Yes'),
    ),
    '#default_value' => $edit['selected'],
    '#description' => t('Set this to <em>Yes</em> if you would like this category to be selected by default.'),
  );
  $form['cid'] = array(
    '#type' => 'value',
    '#value' => $edit['cid'],
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  return $form;
}

/**
 * Validate the mass_contact category edit page form submission.
 */
function mass_contact_admin_edit_validate($form_id, $form_values) {
  if (empty($form_values['category'])) {
    form_set_error('category', t('You must enter a category.'));
  }
  $recipients = $form_values['recipients'];
  foreach ($recipients as $checkr) {
    if ($checkr > 1) {
      return;
    }
  }
  form_set_error('recipients', t('You must enter one or more recipients.'));
}

/**
 * Process the mass_contact category edit page form submission.
 */
function mass_contact_admin_edit_submit($form_id, $form_values) {
  if ($form_values['selected']) {

    // Unselect all other contact categories.
    db_query('UPDATE {mass_contact} SET selected = 0');
  }

  //remove 0s for unselected roles, convert to csv
  $recipients = $form_values['recipients'];

  // if all authenticated users are already added, remove all roles
  if ($recipients[2] == 2) {
    foreach ($recipients as $checkr) {
      if ($checkr > 2) {
        $recipients[$checkr] = 0;
      }
    }
  }

  // remove roles that were not selected
  foreach ($recipients as $recip) {
    if ($recip != 0) {
      $newformrec[] = $recip;
    }
  }
  $form_values['recipients'] = implode(',', $newformrec);
  if (arg(3) == 'add') {
    db_query("INSERT INTO {mass_contact} (category, recipients, reply, weight, selected) VALUES ('%s', '%s', '%s', %d, %d)", $form_values['category'], $form_values['recipients'], $form_values['reply'], $form_values['weight'], $form_values['selected']);
    drupal_set_message(t('Category %category has been added.', array(
      '%category' => $form_values['category'],
    )));
    watchdog('mass_contact', t('Mass Contact form: category %category added.', array(
      '%category' => $form_values['category'],
    )), WATCHDOG_NOTICE, l(t('view'), 'admin/build/mass_contact'));
  }
  else {
    db_query("UPDATE {mass_contact} SET category = '%s', recipients = '%s', reply = '%s', weight = %d, selected = %d WHERE cid = %d", $form_values['category'], $form_values['recipients'], $form_values['reply'], $form_values['weight'], $form_values['selected'], $form_values['cid']);
    drupal_set_message(t('Category %category has been updated.', array(
      '%category' => $form_values['category'],
    )));
    watchdog('mass_contact', t('Mass Contact form: category %category updated.', array(
      '%category' => $form_values['category'],
    )), WATCHDOG_NOTICE, l(t('view'), 'admin/build/mass_contact'));
  }
  return 'admin/build/mass_contact';
}

/**
 * Category delete page.
 */
function mass_contact_admin_delete($cid = NULL) {
  if ($info = db_fetch_object(db_query("SELECT category FROM {mass_contact} WHERE cid = %d", $cid))) {
    $form['category'] = array(
      '#type' => 'value',
      '#value' => $info->category,
    );
    return confirm_form($form, t('Are you sure you want to delete %category?', array(
      '%category' => $info->category,
    )), 'admin/build/mass_contact', t('This action cannot be undone.'), t('Delete'), t('Cancel'));
  }
  else {
    drupal_set_message(t('Category not found.'), 'error');
    drupal_goto('admin/build/mass_contact');
  }
}

/**
 * Process category delete form submission.
 */
function mass_contact_admin_delete_submit($form_id, $form_values) {
  db_query("DELETE FROM {mass_contact} WHERE cid = %d", arg(4));
  drupal_set_message(t('Category %category has been deleted.', array(
    '%category' => $form_values['category'],
  )));
  watchdog('mass_contact', t('Mass Contact form: category %category deleted.', array(
    '%category' => $form_values['category'],
  )), WATCHDOG_NOTICE);
  return 'admin/build/mass_contact';
}
function mass_contact_admin_settings() {
  $form['mass_contact_form_information'] = array(
    '#type' => 'textarea',
    '#title' => t('Additional information for Mass Contact form'),
    '#default_value' => variable_get('mass_contact_form_information', t('Send emails using the following form.')),
    '#description' => t('Information to show on the <a href="@form">Mass Contact page</a>.', array(
      '@form' => url('mass_contact'),
    )),
  );
  $form['mass_contact_HTML_d'] = array(
    '#type' => 'checkbox',
    '#title' => t('Send as HTML by default'),
    '#default_value' => variable_get('mass_contact_HTML_d', 1),
  );
  $form['mass_contact_bcc_d'] = array(
    '#type' => 'checkbox',
    '#title' => t('Send as Bcc (hide recipients) by default'),
    '#default_value' => variable_get('mass_contact_bcc_d', 1),
  );
  $form['mass_contact_nodecc_d'] = array(
    '#type' => 'checkbox',
    '#title' => t('Save a copy as a node by default'),
    '#default_value' => variable_get('mass_contact_nodecc_d', 1),
  );
  $form['mass_contact_optout_d'] = array(
    '#type' => 'checkbox',
    '#title' => t('Allow users to opt-out of mass emails'),
    '#default_value' => variable_get('mass_contact_optout_d', 1),
  );
  $form['mass_contact_recipient_limit'] = array(
    '#type' => 'textfield',
    '#title' => t('Max number of users before breaking up the e-mail'),
    '#size' => 4,
    '#default_value' => variable_get('mass_contact_recipient_limit', 0),
    '#description' => t('This is a workaround for server-side limits on the number of recipients in a single mail message.  Once this limit is reached, the recipient list will be broken up and multiple copies of the message will be sent out until all recipients receive the mail.  Setting this to "0" will turn off this feature.'),
  );
  $form['mass_contact_hourly_threshold'] = array(
    '#type' => 'select',
    '#title' => t('Hourly threshold'),
    '#options' => drupal_map_assoc(array(
      1,
      2,
      3,
      4,
      5,
      6,
      7,
      8,
      9,
      10,
      20,
      30,
      40,
      50,
    )),
    '#default_value' => variable_get('mass_contact_hourly_threshold', 3),
    '#description' => t('The maximum number of Mass Contact form submissions a user can perform per hour.'),
  );
  return system_settings_form($form);
}

/**
 * Implementation of hook_user().
 *
 * Provide form element to opt in to content mailouts.
 */
function mass_contact_user($type, $edit, &$user, $category = NULL) {
  if (variable_get('mass_contact_optout_d', 1) == 1) {
    if ($type == 'register' || $type == 'form' && $category == 'account') {
      $form['mail'] = array(
        '#type' => 'fieldset',
        '#title' => t('Group e-mail settings'),
        '#weight' => 5,
        '#collapsible' => TRUE,
      );
      $form['mail']['mass_contact_optout'] = array(
        '#type' => 'checkbox',
        '#title' => t('Opt-out of Mass E-mails'),
        '#default_value' => $edit['mass_contact_optout'],
        '#description' => t('Allows you to opt-out of group e-mails from privileged users. Note that site administrators are able to include you in mass e-mails even if you choose not to enable this feature, and the ability to opt-out may be removed by the administrator at any time.'),
      );
      return $form;
    }
    elseif ($type == 'validate') {
      return array(
        'mass_contact_optout' => $edit['mass_contact_optout'],
      );
    }
  }
}

/**
 * mail page
 */
function mass_contact_site_page() {
  global $user;
  if (!user_access('administer site configuration') && !flood_is_allowed('mass_contact', variable_get('mass_contact_hourly_threshold', 3))) {
    $output = t("You cannot send more than %number messages per hour. Please try again later.", array(
      '%number' => variable_get('mass_contact_hourly_threshold', 3),
    ));
  }
  else {
    $output = drupal_get_form('mass_contact_mail_page');
  }
  return $output;
}
function mass_contact_mail_page() {
  global $user;
  $result = db_query('SELECT cid, category, selected FROM {mass_contact} ORDER BY weight, category');
  while ($category = db_fetch_object($result)) {
    $categories[$category->cid] = $category->category;
    if ($category->selected) {
      $default_category = $category->cid;
    }
  }
  if (count($categories) > 0) {
    $form['#attributes'] = array(
      'enctype' => "multipart/form-data",
    );
    $form['#token'] = $user->name . $user->mail;
    $form['contact_information'] = array(
      '#value' => filter_xss_admin(variable_get('mass_contact_form_information', t('Send an email message using the contact form below.'))),
    );
    $form['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Your name'),
      '#maxlength' => 255,
      '#default_value' => $user->uid ? $user->name : '',
      '#required' => TRUE,
    );
    $form['mail'] = array(
      '#type' => 'textfield',
      '#title' => t('Your e-mail address'),
      '#maxlength' => 255,
      '#default_value' => $user->uid ? $user->mail : '',
      '#required' => TRUE,
    );
    $form['subject'] = array(
      '#type' => 'textfield',
      '#title' => t('Subject'),
      '#maxlength' => 255,
      '#required' => TRUE,
    );

    // If there is more than one category available and no default category has been selected,
    // prepend a default placeholder value.
    if (!isset($default_category)) {
      $categories = array(
        t('--'),
      ) + $categories;
    }
    $form['cid'] = array(
      '#type' => 'select',
      '#title' => t('Category'),
      '#default_value' => $default_category,
      '#options' => $categories,
      '#required' => TRUE,
    );
    if (variable_get('mass_contact_optout_d', 1) == 1) {

      // allow to override or respect opt-outs if admin, otherwise use default
      if (user_access('administer site configuration')) {
        $form['optout'] = array(
          '#type' => 'checkbox',
          '#title' => t('Respect user opt-outs'),
          '#default_value' => 1,
        );
      }
      else {
        $form['optout'] = array(
          '#type' => 'hidden',
          '#default_value' => 1,
        );
      }
    }
    $form['message'] = array(
      '#type' => 'textarea',
      '#title' => t('Message'),
      '#required' => TRUE,
    );
    if (user_access('send mass contact attachments')) {
      $form['attachment'] = array(
        '#type' => 'file',
        '#title' => t('Attachment'),
        '#size' => 40,
      );
    }
    $form['html'] = array(
      '#type' => 'checkbox',
      '#title' => t('Send as HTML'),
      '#default_value' => variable_get('mass_contact_HTML_d', 0),
    );
    $form['bcc'] = array(
      '#type' => 'checkbox',
      '#title' => t('Send as Bcc (hide recipients)'),
      '#default_value' => variable_get('mass_contact_bcc_d', 1),
    );
    if (user_access('choose whether to archive mass contact messages')) {
      $form['nodecc'] = array(
        '#type' => 'checkbox',
        '#title' => t('Save a copy as a node'),
        '#default_value' => variable_get('mass_contact_nodecc_d', 1),
      );
    }
    else {
      $form['nodecc'] = array(
        '#type' => 'hidden',
        '#default_value' => variable_get('mass_contact_nodecc_d', 1),
      );
    }
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Send e-mail'),
    );
  }
  else {
    $form['error'] = array(
      '#value' => '<p><b>' . t('You must create at least one category before using this form.' . '</b>'),
    );
  }
  if (user_access('administer site configuration')) {
    $form['tasklist'] = array(
      '#type' => 'fieldset',
      '#title' => t('Related tasks'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#prefix' => '<p>',
    );
    $form['tasklist']['list'] = array(
      '#value' => '<p>' . t('1. <a href="/admin/build/mass_contact/add">Add a category</a><br/>2. <a href="/admin/build/mass_contact">View categories</a><br/>3. <a href="/admin/build/mass_contact/settings">Configure the module</a><br/>4. <a href="/admin/user/access#module-mass_contact">Set permissions</a><br />5. <a href="/mass_contact">Send mass e-mail</a><br />6. <a href="/admin/help/mass_contact">Help</a>') . '</p>',
    );
  }
  return $form;
}

/**
 * Validate the site-wide contact page form submission.
 */
function mass_contact_mail_page_validate($form_id, $form_values) {
  if (!$form_values['cid']) {
    form_set_error('category', t('You must select a valid category.'));
  }
  if (!valid_email_address($form_values['mail'])) {
    form_set_error('mass_contact', t('You must enter a valid e-mail address.'));
  }
}

/**
 * Process the mail page form submission.
 */
function mass_contact_mail_page_submit($form_id, $form_values) {
  global $user;
  $bcc = $form_values['bcc'];
  $nodecc = $form_values['nodecc'];
  $send_error = 0;

  // E-mail address of the sender: as the form field is a text field,
  // all instances of \r and \n have been automatically stripped from it.
  $from = $form_values['mail'];
  $uid = '';
  $ctype = '';

  // create headers based on presence or absence of attachment
  if ($_FILES['files']['size']['attachment'] > 0) {
    $uid = md5(uniqid(time()));
    $headers['Content-Type'] = "multipart/mixed; boundary=\"" . $uid . "\"";
    if ($form_values['html'] == 1) {
      $ctype = "Content-Type: text/html; charset=\"iso-8859-1\"\nContent-Transfer-Encoding: 8bit\n";
    }
    else {
      $ctype = "Content-Type: text/plain; charset=\"iso-8859-1\"\nContent-Transfer-Encoding: 8bit\n";
    }
    $message[] = "--" . $uid . "\n" . $ctype;
  }
  else {
    if ($form_values['html'] == 1) {
      $headers['Content-Type'] = 'text/html';
    }
  }

  // Compose the body:
  $message[] = t("!name sent out a mass e-mail from !site.", array(
    '!name' => $form_values['name'],
    '!site' => url(NULL, NULL, NULL, TRUE),
  ));
  if ($form_values['html'] == 1) {
    $message[] = "<p>";
  }
  $message[] = wordwrap($form_values['message']);

  // add attachment
  if ($_FILES['files']['size']['attachment'] > 0) {
    $message[] = "--" . $uid . "\n" . "Content-Transfer-Encoding: base64\nContent-Type: application/octet-stream; name=\"" . basename($_FILES['files']['name']['attachment']) . "\"\n" . "Content-Disposition: attachment; filename=\"" . basename($_FILES['files']['name']['attachment']) . "\"\n\n" . chunk_split(base64_encode(file_get_contents($_FILES['files']['tmp_name']['attachment']))) . "\n\n" . "--" . $uid . "--";
  }

  // Load the category information:
  $contact = db_fetch_object(db_query("SELECT * FROM {mass_contact} WHERE cid = %d", $form_values['cid']));

  // Format the category:
  $subject = t('[!category] !subject', array(
    '!category' => $contact->category,
    '!subject' => $form_values['subject'],
  ));

  // Prepare the body:
  $body = implode("\n\n", $message);

  // check for opt-out unless overridden by admin
  if (variable_get('mass_contact_optout_d', 1) == 1) {
    $allow_oo = $form_values['optout'];
    if ($allow_oo == 1) {
      $ooresult = db_query('SELECT uid FROM {users} WHERE status != 0');
      while ($oouid = db_fetch_object($ooresult)) {
        $account = user_load(array(
          'uid' => $oouid->uid,
          'status' => 1,
        ));
        if ($account->mass_contact_optout) {
          $uidoo = $oouid->uid;
          $uidooa[$uidoo] = 1;
        }
      }
    }
  }

  // Create the recipients
  $roles = explode(',', $contact->recipients);
  foreach ($roles as $r) {
    if ($r == 2) {

      // all users
      $recipients = db_query("SELECT name, mail, uid FROM {users} WHERE status != 0 AND uid > 0");
      while ($obj = db_fetch_object($recipients)) {
        $rname = $obj->name;
        $rmail = $obj->mail;
        $ruid = $obj->uid;
        if ($rname != $user->name && !$uidooa[$ruid]) {
          $recipientouta[$rname] = $rmail;
        }
      }
      break;
    }
    else {

      //get from users_roles, then role -> user
      $uids = db_query("SELECT uid FROM {users_roles} WHERE rid = %d", $r);
      while ($obj = db_fetch_object($uids)) {
        $ruid = $obj->uid;
        $userobj = db_fetch_object(db_query("SELECT name, mail FROM {users} WHERE uid = %d", $ruid));
        $rname = $userobj->name;
        $rmail = $userobj->mail;
        if ($rname != $user->name && !$uidooa[$ruid]) {
          $recipientouta[$rname] = $rmail;
        }
      }
    }
  }

  // check for empty
  if (count($recipientouta) == 0) {
    drupal_set_message(t('There are no users in this category.	Mail not sent.'));
    return '';
  }

  // check for recipient limit and break up 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 . ">";
      $recipient_temp[] = $rnamea . " <" . $rmaila . ">";
      $countrecip = count($recipient_temp);
      if ($countrecip == $recip_limit) {
        $recipient_send = implode(', ', $recipient_temp);

        // set bcc
        if ($bcc == 1) {

          // hidden recipients
          $headers['Bcc'] = $recipient_send;
          $to = $from;
        }
        else {
          $to = $recipient_send;
        }
        $ccc++;

        // Send the e-mail to the recipients:
        $success = drupal_mail('mass-contact-page-mail', $to, $subject, $body, $from, $headers);
        if ($success) {
          drupal_set_message(t('[Success] Send #!ccc: -emails', array(
            '!ccc' => $ccc,
            '-emails' => $recipient_send,
          )));
        }
        else {
          $send_error++;
        }

        // reset array
        $recipient_temp = array();
        $countrecip = 0;
      }
    }

    // send remainder
    if ($countrecip != 0) {
      $recipient_send = implode(', ', $recipient_temp);
      if ($bcc == 1) {

        // hidden recipients
        $headers['Bcc'] = $recipient_send;
        $to = $from;
      }
      else {
        $to = $recipient_send;
      }
      $ccc++;

      // Send the e-mail to the recipients:
      $success = drupal_mail('mass-contact-page-mail', $to, $subject, $body, $from, $headers);
      if ($success) {
        drupal_set_message(t('[Success] Send Remainder: -emails', array(
          '-emails' => $recipient_send,
        )));
      }
      else {
        $send_error++;
      }
    }
    $total_recip = count($recipientout);
    $recipientout = implode(', ', $recipientout);
  }
  else {
    foreach ($recipientouta as $rnamea => $rmaila) {
      $recipientout[] = $rnamea . " <" . $rmaila . ">";
    }
    $total_recip = count($recipientout);
    $recipientout = implode(', ', $recipientout);

    // set bcc
    if ($bcc == 1) {

      // hidden recipients
      $headers['Bcc'] = $recipientout;
      $to = $from;
    }
    else {
      $to = $recipientout;
    }

    // Send the e-mail to the recipients:
    $success = drupal_mail('mass-contact-page-mail', $to, $subject, $body, $from, $headers);
    if ($success) {
      drupal_set_message(t('[Success] Send Once: -emails', array(
        '-emails' => $recipientout,
      )));
    }
    else {
      $send_error++;
    }
  }

  // if no errors
  if ($send_error == 0) {

    // Save as a node
    if ($nodecc == 1) {
      $check = db_fetch_array(db_query('SELECT * FROM {node_type} WHERE type = "mass_contact"'));
      if (!$check) {

        // add node type
        $info->type = "mass_contact";
        $info->name = "Mass Contact Message";
        $info->module = "node";
        $info->has_title = 1;
        $info->title_label = "Subject";
        $info->has_body = 1;
        $info->body_label = "Message Body";
        $info->description = "Archived copy of mass e-mails sent from this site";
        $info->help = "";
        $info->min_word_count = 0;
        $info->custom = 1;
        $info->modified = 0;
        $info->locked = 0;
        $info->old_type = 'mass_contact';
        db_query("INSERT INTO {node_type} (type, name, module, has_title, title_label, has_body, body_label, description, help, min_word_count, custom, modified, locked, orig_type) VALUES ('%s', '%s', '%s', %d, '%s', %d, '%s', '%s', '%s', %d, %d, %d, %d, '%s')", $info->type, $info->name, $info->module, $info->has_title, $info->title_label, $info->has_body, $info->body_label, $info->description, $info->help, $info->min_word_count, $info->custom, $info->modified, $info->locked, $info->orig_type);
        module_invoke_all('node_type', 'insert', $info);
        drupal_set_message(t('Node type created.'));
      }
      $save = mass_contact_save_node($subject, $body, $recipientout, $contact->category, $roles, $user->uid);
    }

    // Get new node nid
    $nidobj = db_fetch_object(db_query("SELECT nid FROM {node} WHERE type = 'mass_contact' ORDER BY changed DESC"));
    $nid = $nidobj->nid;

    // Log the operation:
    flood_register_event('mass_contact');
    watchdog('mass_contact', t('%name-from sent an e-mail to the %category group.', array(
      '%name-from' => $form_values['name'] . " <{$from}>",
      '%category' => $contact->category,
    )));

    // Update user:
    drupal_set_message(t('Message sent successfully to !total users: -emails', array(
      '!total' => $total_recip,
      '-emails' => $recipientout,
    )));
    if ($save == TRUE) {

      // log node creation:
      watchdog('content', t('Mass Contact Content added %title.', array(
        '%title' => $subject,
      )), WATCHDOG_NOTICE, l(t('view'), "node/{$nid}"));

      // Update user:
      drupal_set_message(t('A carbon copy has been created as a node.'));
    }
  }
  else {
    drupal_set_message(t('!errors errors encountered sending message.  Please check the logs and try again.', array(
      '!errors' => $send_error,
    )));
  }

  // Jump to home page rather than back to mail page to avoid contradictory messages if flood control has been activated.
  return '';
}

/**
 * Save the sent mail as a node.
 */
function mass_contact_save_node($subject, $body, $recipients, $category, $roles, $uid) {

  // get role names
  foreach ($roles as $r) {
    $roletemp = db_fetch_object(db_query("SELECT name FROM {role} WHERE rid = %d", $r));
    $rolesenta[] = $roletemp->name;
  }
  $rolesent = implode(', ', $rolesenta);
  $node->title = $subject;
  $node->body = '<p><i>' . t('Category: ') . $category . '</i><p><i>' . t('Roles: ') . $rolesent . '</i><p><i>' . t('Recipients: ') . $recipients . '</i><p>' . $body;
  $node->teaser = node_teaser($node->body);
  $node->type = 'mass_contact';
  $node->uid = $uid;
  $node->format = 3;
  $node->status = 0;
  $node->comment = 2;
  $node->promote = 0;
  $node->sticky = 0;
  node_save($node);
  return TRUE;
}

Functions

Namesort descending Description
mass_contact_admin_categories Categories/list tab.
mass_contact_admin_delete Category delete page.
mass_contact_admin_delete_submit Process category delete form submission.
mass_contact_admin_edit Category edit page.
mass_contact_admin_edit_submit Process the mass_contact category edit page form submission.
mass_contact_admin_edit_validate Validate the mass_contact category edit page form submission.
mass_contact_admin_settings
mass_contact_help Implementation of hook_help().
mass_contact_mail_page
mass_contact_mail_page_submit Process the mail page form submission.
mass_contact_mail_page_validate Validate the site-wide contact page form submission.
mass_contact_menu Implementation of hook_menu().
mass_contact_perm Implementation of hook_perm
mass_contact_save_node Save the sent mail as a node.
mass_contact_site_page mail page
mass_contact_user Implementation of hook_user().