You are here

function mimemail_html_body in Mime Mail 6

Same name and namespace in other branches
  1. 5 mimemail.inc \mimemail_html_body()
  2. 7 mimemail.inc \mimemail_html_body()

Generate a multipart message body with a text alternative for some HTML text.

The first MIME part is a multipart/alternative containing MIME-encoded sub-parts for HTML and plaintext. Each subsequent part is the required image/attachment.

Parameters

$body: An HTML message body

$subject: The message subject

$plaintext: TRUE if the recipient prefers plaintext-only messages. Defaults to FALSE.

Return value

An array containing the headers and the body of the message.

1 call to mimemail_html_body()
mimemail_prepare_message in ./mimemail.module
Default engine's prepare function.

File

./mimemail.inc, line 373
Common mail functions for sending e-mail. Originally written by Gerhard.

Code

function mimemail_html_body($body, $subject, $plaintext = FALSE, $text = NULL, $attachments = array()) {
  if (empty($text)) {

    // @todo Remove once filter_xss() can handle direct descendant selectors in inline CSS.
    // @see http://drupal.org/node/1116930, http://drupal.org/node/370903
    // Pull out the message body.
    preg_match('|<body.*?</body>|mis', $body, $matches);
    $text = drupal_html_to_text($matches[0]);
  }
  if ($plaintext) {

    // Plain mail without attachment.
    if (empty($attachments)) {
      $content_type = 'text/plain';
      return array(
        'body' => $text,
        'headers' => array(
          'Content-Type' => 'text/plain; charset=utf-8',
        ),
      );
    }
    else {
      $content_type = 'multipart/mixed';
      $parts = array(
        array(
          'content' => $text,
          'Content-Type' => 'text/plain; charset=utf-8',
        ),
      );
    }
  }
  else {
    $content_type = 'multipart/mixed';
    $text_part = array(
      'Content-Type' => 'text/plain; charset=utf-8',
      'content' => $text,
    );

    // Expand all local links.
    $pattern = '/(<a[^>]+href=")([^"]*)/mi';
    $body = preg_replace_callback($pattern, '_mimemail_expand_links', $body);
    $mime_parts = mimemail_extract_files($body);
    $content = array(
      $text_part,
      array_shift($mime_parts),
    );
    $content = mimemail_multipart_body($content, 'multipart/alternative', TRUE);
    $parts = array(
      array(
        'Content-Type' => $content['headers']['Content-Type'],
        'content' => $content['body'],
      ),
    );
    if ($mime_parts) {
      $parts = array_merge($parts, $mime_parts);
      $content = mimemail_multipart_body($parts, 'multipart/related; type="multipart/alternative"', TRUE);
      $parts = array(
        array(
          'Content-Type' => $content['headers']['Content-Type'],
          'content' => $content['body'],
        ),
      );
    }
  }
  if (is_array($attachments) && !empty($attachments)) {
    foreach ($attachments as $a) {
      $a = (object) $a;

      // Check the list parameter if its set or ignore it (Upload module support).
      if (!isset($a->list) || $a->list) {
        _mimemail_file($a->filepath, $a->filecontent, $a->filename, $a->filemime, 'attachment');
        $parts = array_merge($parts, _mimemail_file());
      }
    }
  }
  return mimemail_multipart_body($parts, $content_type);
}