You are here

public static function MimeMailFormatHelper::mimeMailExtractFiles in Mime Mail 8

Extracts links to local images from HTML documents.

Parameters

string $html: A string containing the HTML source of the message.

Return value

array An array containing the document body and the extracted files, structured like the following:


  [
    [
      'name' => document name,
      'content' => html text, local image urls replaced by Content-IDs,
      'Content-Type' => 'text/html; charset=utf-8',
    ],
    [
      'name' => file name,
      'file' => reference to local file,
      'Content-ID' => generated Content-ID,
      'Content-Type' => derived using mime_content_type if available, educated guess otherwise,
    ],
  ]
  
1 call to MimeMailFormatHelper::mimeMailExtractFiles()
MimeMailFormatHelper::mimeMailHtmlBody in src/Utility/MimeMailFormatHelper.php
Generates a multipart message body with a plaintext alternative.

File

src/Utility/MimeMailFormatHelper.php, line 251

Class

MimeMailFormatHelper
Utility methods for formatting MIME-encoded email messages.

Namespace

Drupal\mimemail\Utility

Code

public static function mimeMailExtractFiles($html) {
  $pattern = '/(<link[^>]+href=[\'"]?|<object[^>]+codebase=[\'"]?|@import |[\\s]src=[\'"]?)([^\'>"]+)([\'"]?)/mis';
  $content = preg_replace_callback($pattern, function ($matches) {

    // matches[1] is one of:
    // - A link tag starting with the <, up to and including the starting
    //   quote of the href attribute. OR,
    // - An object tag starting with the <, up to and including the starting
    //   quote of the codebase attribute. OR,
    // - A CSS @import rule starting with the @, up to and including the
    //   starting quote of the url property. OR,
    // - A tag with a src attribute, starting with the whitespace before the
    //   src, up to and including the starting quote of the src attribute.
    //
    // matches[2] is everything between quotes after one of the above
    // matches.
    //
    // matches[3] is the trailing quote.
    return stripslashes($matches[1]) . self::mimeMailFile($matches[2]) . stripslashes($matches[3]);
  }, $html);
  $encoding = '8Bit';
  $body = explode("\n", $content);
  foreach ($body as $line) {
    if (mb_strlen($line) > 998) {
      $encoding = 'base64';
      break;
    }
  }
  if ($encoding == 'base64') {
    $content = rtrim(chunk_split(base64_encode($content)));
  }
  $document[] = [
    'Content-Type' => "text/html; charset=utf-8",
    'Content-Transfer-Encoding' => $encoding,
    'content' => $content,
  ];
  $files = static::mimeMailFile();
  return array_merge($document, $files);
}