You are here

public static function MimeMailFormatHelper::mimeMailHtmlBody in Mime Mail 8

Generates a multipart message body with a plaintext alternative.

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 MimeMailFormatHelper::mimeMailHtmlBody()
MimeMail::prepareMessage in src/Plugin/Mail/MimeMail.php
Prepares the message for sending.

File

src/Utility/MimeMailFormatHelper.php, line 139

Class

MimeMailFormatHelper
Utility methods for formatting MIME-encoded email messages.

Namespace

Drupal\mimemail\Utility

Code

public static function mimeMailHtmlBody($body, $subject, $plain = FALSE, $plaintext = NULL, array $attachments = []) {
  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 = MailFormatHelper::htmlToText($matches[0]);
  }
  if ($plain) {

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

    // Expand all local links.
    $pattern = '/(<a[^>]+href=")([^"]*)/mi';
    $body = preg_replace_callback($pattern, function ($matches) {

      // matches[1] is the anchor tag starting with the <, up to and
      // including the starting quote of the href attribute.
      //
      // matches[2] is everything between quotes in the href attribute
      // of an anchor tag.
      return $matches[1] . self::mimeMailUrl($matches[2]);
    }, $body);
    $mime_parts = static::mimeMailExtractFiles($body);
    $content = [
      $plaintext_part,
      array_shift($mime_parts),
    ];
    $content = static::mimeMailMultipartBody($content, 'multipart/alternative', TRUE);
    $parts[] = [
      'Content-Type' => $content['headers']['Content-Type'],
      'content' => $content['body'],
    ];
    if ($mime_parts) {
      $parts = array_merge($parts, $mime_parts);
      $content = static::mimeMailMultipartBody($parts, 'multipart/related; type="multipart/alternative"', TRUE);
      $parts[] = [
        '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;

      // Add this attachment to the cumulative manifest being built by
      // mimeMailFile().
      static::mimeMailFile($path, $content, $name, $type, 'attachment');
    }

    // Fetch the cumulative manifest from mimeMailFile() and merge it with
    // the other parts of this message.
    $parts = array_merge($parts, static::mimeMailFile());
  }
  return static::mimeMailMultipartBody($parts, $content_type);
}