You are here

function mimemail_html_body in Mime Mail 7

Same name and namespace in other branches
  1. 5 mimemail.inc \mimemail_html_body()
  2. 6 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 or attachment.

Parameters

string $body: The HTML message body.

string $subject: The message subject.

bool $plain: (optional) Whether the recipient prefers plaintext-only messages. Defaults to FALSE.

string $plaintext: (optional) The plaintext message body.

array $attachments: (optional) The files to be attached to the message.

Return value

array An associative array containing the following elements:

  • body: A string containing the MIME-encoded multipart body of a mail.
  • headers: An array that includes some headers for the mail to be sent.
1 call to mimemail_html_body()
mimemail_prepare_message in ./mimemail.module
Prepares the message for sending.

File

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

Code

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

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

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

    // 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(
      $plaintext_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;
      $path = isset($a->uri) ? $a->uri : (isset($a->filepath) ? $a->filepath : NULL);
      $content = isset($a->filecontent) ? $a->filecontent : NULL;
      $name = isset($a->filename) ? $a->filename : NULL;
      $type = isset($a->filemime) ? $a->filemime : NULL;
      _mimemail_file($path, $content, $name, $type, 'attachment');
      $parts = array_merge($parts, _mimemail_file());
    }
  }
  return mimemail_multipart_body($parts, $content_type);
}