You are here

function _mass_contact_mail in Mass Contact 6

2 calls to _mass_contact_mail()
mass_contact_mail_page_submit in ./mass_contact.module
Processes the main Mass Contact mail form.
_mass_contact_send_message in ./mass_contact.module
Sends the message.

File

./mass_contact.module, line 2421
This is the main code file for the Mass Contact module. This module enables users to contact multiple users through selected roles.

Code

function _mass_contact_mail($module, $key, $to, $language, $params = array(), $from = NULL, $send = TRUE) {
  $default_from = variable_get('site_mail', ini_get('sendmail_from'));

  // Bundle up the variables into a structured array for altering.
  $message = array(
    'id' => $module . '_' . $key,
    'to' => $to,
    'from' => isset($from) ? $from : $default_from,
    'language' => $language,
    'params' => $params,
    'subject' => '',
    'body' => array(),
  );

  // Build the default headers
  $headers = array(
    'MIME-Version' => '1.0',
    'Content-Type' => 'text/plain; charset=UTF-8; format=flowed; delsp=yes',
    'Content-Transfer-Encoding' => '8Bit',
    'X-Mailer' => 'Drupal',
  );
  if ($default_from) {

    // To prevent e-mail from looking like spam, the addresses in the Sender and
    // Return-Path headers should have a domain authorized to use the originating
    // SMTP server. Errors-To is redundant, but shouldn't hurt.
    $headers['From'] = $headers['Sender'] = $headers['Return-Path'] = $headers['Errors-To'] = $default_from;
  }
  if ($from) {
    $headers['From'] = $from;
  }
  $message['headers'] = $headers;

  // Build the e-mail (get subject and body, allow additional headers) by
  // invoking hook_mail() on this module. We cannot use module_invoke() as
  // we need to have $message by reference in hook_mail().
  if (function_exists($function = $module . '_mail')) {
    $function($key, $message, $params);
  }

  // Invoke hook_mail_alter() to allow all modules to alter the resulting e-mail.
  drupal_alter('mail', $message);

  //////////////////////////////////////////////////////////////////////////

  // Eliminated the destruction of multipart/mixed e-mails by removing    //
  // the concatenation and wrapping of the e-mail body.                   //
  // http://drupal.org/node/473838                                        //

  //////////////////////////////////////////////////////////////////////////

  // Optionally send e-mail.
  if ($send) {
    $message['result'] = drupal_mail_send($message);

    // Log errors
    if (!$message['result']) {
      watchdog('mail', 'Error sending e-mail (from %from to %to).', array(
        '%from' => $message['from'],
        '%to' => $message['to'],
      ), WATCHDOG_ERROR);
      drupal_set_message(t('Unable to send e-mail. Please contact the site administrator if the problem persists.'), 'error');
    }
  }
  return $message;
}