You are here

protected function Messaging_HTML_Mail_Method::mail_build in Messaging 7

Rebuild message in Drupal mail format, right before sending

Note that our 'body' element is a renderable array, not like drupal_mail() This may clash with some module altering the mail

This mimics drupal_mail for finest access to properties

Parameters

$address: Email address to send to

$message: Message object

Overrides Messaging_Mail_Method::mail_build

File

messaging_htmlmail/messaging_htmlmail.inc, line 31
Drupal Messaging Framework - Send_Method class file

Class

Messaging_HTML_Mail_Method
Base class for mail sending methods

Code

protected function mail_build($address, $message) {
  $params = $this
    ->message_params($message);
  $template = $message
    ->get_template();
  $mail = array(
    'headers' => $this
      ->mail_headers($message, $params),
    'id' => $message->module . '_' . $message->key,
    'module' => $message->module,
    'key' => $message->key,
    'to' => $params['send_bcc'] ? '' : $address,
    'from' => isset($params['from']) ? $params['from'] : $params['default_from'],
    'language' => $message
      ->get_language(),
    'params' => $params,
    'subject' => $message
      ->get_subject(),
    // Bundle up the variables into a structured array for altering.
    'body' => $template
      ->set_format(MESSAGING_FORMAT_HTML)
      ->build('body'),
    'body_plain' => $template
      ->set_format(MESSAGING_FORMAT_PLAIN)
      ->render('body'),
    'attachments' => $message
      ->get_files(),
  );
  if ($params['send_bcc']) {
    $mail['headers']['Bcc'] = $address;
  }

  // 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 = $message->module . '_mail')) {
    $function($message->key, $mail, $params);
  }

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

  // Invoke drupal_mail without sending, then override headers
  return $mail;
}