You are here

function mimemail_prepare_message in Mime Mail 7

Same name and namespace in other branches
  1. 6 mimemail.module \mimemail_prepare_message()

Prepares the message for sending.

Parameters

array $message: An array containing the message data. The optional parameters are:

  • plain: Whether to send the message as plaintext only or HTML. If evaluates to TRUE, then the message will be sent as plaintext.
  • plaintext: Optional plaintext portion of a multipart email.
  • attachments: An array of arrays which describe one or more attachments. Existing files can be added by path, dinamically-generated files can be added by content. The internal array contains the following elements:

    • filepath: Relative Drupal path to an existing file (filecontent is NULL).
    • filecontent: The actual content of the file (filepath is NULL).
    • filename: The filename of the file.
    • filemime: The MIME type of the file.

    The array of arrays looks something like this: Array ( [0] => Array ( [filepath] => '/sites/default/files/attachment.txt' [filecontent] => 'My attachment.' [filename] => 'attachment.txt' [filemime] => 'text/plain' ) )

Return value

array All details of the message.

2 calls to mimemail_prepare_message()
MimeMailSystem::format in includes/mimemail.mail.inc
Concatenate and wrap the e-mail body for HTML mails.
MimeMailWebTestCase::testMailPreparationPlaintextBodyConversion in tests/mimemail.test

File

./mimemail.module, line 341
Component module for sending Mime-encoded emails.

Code

function mimemail_prepare_message($message) {
  module_load_include('inc', 'mimemail');
  $module = $message['module'];
  $key = $message['key'];
  $to = $message['to'];
  $from = $message['from'];
  $subject = $message['subject'];
  $body = $message['body'];
  $headers = isset($message['params']['headers']) ? $message['params']['headers'] : array();
  $plain = isset($message['params']['plain']) ? $message['params']['plain'] : NULL;
  $plaintext = isset($message['params']['plaintext']) ? $message['params']['plaintext'] : NULL;
  $attachments = isset($message['params']['attachments']) ? $message['params']['attachments'] : array();
  $site_name = variable_get('site_name', 'Drupal');
  $site_mail = variable_get('site_mail', ini_get('sendmail_from'));
  $simple_address = variable_get('mimemail_simple_address', 0);

  // Override site mails default sender when using default engine.
  if ((empty($from) || $from == $site_mail) && variable_get('mimemail_engine', 'mimemail') == 'mimemail') {
    $mimemail_name = variable_get('mimemail_name', $site_name);
    $mimemail_mail = variable_get('mimemail_mail', $site_mail);
    $from = array(
      'name' => !empty($mimemail_name) ? $mimemail_name : $site_name,
      'mail' => !empty($mimemail_mail) ? $mimemail_mail : $site_mail,
    );
  }

  // Body is empty, this is a plaintext message.
  if (empty($body)) {
    $plain = TRUE;
  }
  elseif (is_null($plain)) {
    if (is_object($to) && isset($to->data['mimemail_textonly'])) {
      $plain = $to->data['mimemail_textonly'];
    }
    elseif (is_string($to) && valid_email_address($to)) {
      if (is_object($account = user_load_by_mail($to)) && isset($account->data['mimemail_textonly'])) {
        $plain = $account->data['mimemail_textonly'];

        // Might as well pass the user object to the address function.
        $to = $account;
      }
    }
  }

  // Removing newline character introduced by _drupal_wrap_mail_line();
  $subject = str_replace(array(
    "\n",
  ), '', trim(drupal_html_to_text($subject)));
  $hook = array(
    'mimemail_message__' . $key,
    'mimemail_message__' . $module . '__' . $key,
  );
  $variables = array(
    'module' => $module,
    'key' => $key,
    'recipient' => $to,
    'subject' => $subject,
    'body' => $body,
    'message' => $message,
  );
  $body = theme($hook, $variables);
  foreach (module_implements('mail_post_process') as $module) {
    $function = $module . '_mail_post_process';
    $function($body, $key);
  }
  $plain = $plain || variable_get('mimemail_textonly', 0);
  $from = mimemail_address($from);
  $mail = mimemail_html_body($body, $subject, $plain, $plaintext, $attachments);
  $headers = array_merge($message['headers'], $headers, $mail['headers']);
  $message['to'] = mimemail_address($to, $simple_address);
  $message['from'] = $from;
  $message['subject'] = $subject;
  $message['body'] = $mail['body'];
  $message['headers'] = mimemail_headers($headers, $from);
  return $message;
}