You are here

function mimemail_prepare_message in Mime Mail 6

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

Default engine's prepare function.

Parameters

Same parameters as mimemail() except $send.:

Return value

The $message array structure containing all details of the message.

1 call to mimemail_prepare_message()
mimemail in ./mimemail.module
Compose and optionally send a MIME-encoded e-mail message.

File

./mimemail.module, line 151
Component module for sending MIME-encoded e-mails.

Code

function mimemail_prepare_message($sender, $recipient, $subject, $body, $plaintext = NULL, $headers = array(), $text = NULL, $attachments = array(), $mailkey = '') {
  module_load_include('inc', 'mimemail');
  $site_name = variable_get('site_name', 'Drupal');
  $site_mail = variable_get('site_mail', ini_get('sendmail_from'));

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

  // Body is empty, this is a plaintext message.
  if (empty($body)) {
    $plaintext = 1;
  }
  elseif (is_null($plaintext)) {
    if (is_object($recipient)) {
      if (isset($recipient->mimemail_textonly)) {
        $plaintext = $recipient->mimemail_textonly;
      }
    }
    elseif (is_string($recipient) && valid_email_address($recipient)) {
      if (is_object($r = user_load(array(
        'mail' => $recipient,
      ))) && isset($r->mimemail_textonly)) {
        $plaintext = $r->mimemail_textonly;
        $recipient = $r;

        // Pass the user object to the address function.
      }
    }
  }
  $subject = str_replace(array(
    " \n",
    "\n",
  ), '', trim(drupal_html_to_text($subject)));
  $body = theme(array(
    'mimemail_message__' . $mailkey,
    'mimemail_message',
  ), $subject, $body, $mailkey, $recipient);
  foreach (module_implements('mail_post_process') as $module) {
    $function = $module . '_mail_post_process';
    $function($body, $mailkey);
  }
  $plaintext = $plaintext || variable_get('mimemail_textonly', 0);
  $sender = mimemail_address($sender);
  $subject = mime_header_encode($subject);
  $mail = mimemail_html_body($body, $subject, $plaintext, $text, $attachments);
  $headers = array_merge($headers, $mail['headers']);
  $message = array(
    'address' => mimemail_address($recipient),
    'subject' => $subject,
    'body' => $mail['body'],
    'sender' => $sender,
    'headers' => mimemail_headers($headers, $sender),
  );
  return $message;
}