You are here

function mass_contact_mail_page_submit in Mass Contact 5

Same name and namespace in other branches
  1. 5.2 mass_contact.module \mass_contact_mail_page_submit()
  2. 6 mass_contact.module \mass_contact_mail_page_submit()
  3. 7 mass_contact.page.inc \mass_contact_mail_page_submit()

Process the mail page form submission.

File

./mass_contact.module, line 463
Enables mass contact form to selected roles.

Code

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 '';
}