public function PHPMailer::msgHTML

5.x PHPMailer.php public PHPMailer::msgHTML($message, $basedir = '', $advanced = false)

Create a message body from an HTML string. Automatically inlines images and creates a plain-text version by converting the HTML, overwriting any existing values in Body and AltBody. Do not source $message content from user input! $basedir is prepended when handling relative URLs, e.g. <img src="/images/a.png"> and must not be empty will look for an image file in $basedir/images/a.png and convert it to inline. If you don't provide a $basedir, relative paths will be left untouched (and thus probably break in email) Converts data-uri images into embedded attachments. If you don't want to apply these transformations to your HTML, just set Body and AltBody directly.

Parameters

string $message HTML message string:

string $basedir Absolute path to a base directory to prepend to relative paths to images:

bool|callable $advanced Whether to use the internal HTML to text converter: or your own custom converter @see PHPMailer::html2text()

Return value

string $message The transformed message Body

File

inc/PHPMailer/src/PHPMailer.php, line 3864

Class

PHPMailer
PHPMailer - PHP email creation and transport class.

Namespace

PHPMailer\PHPMailer

Code

public function msgHTML($message, $basedir = '', $advanced = false) 
 {
  preg_match_all('/(src|background)=["\'](.*)["\']/Ui', $message, $images);
  if (array_key_exists(2, $images)) {
    if (strlen($basedir) > 1 && '/' != substr($basedir, -1)) {
      // Ensure $basedir has a trailing /
      $basedir .= '/';
    }
    foreach ($images [2] as $imgindex => $url) {
      // Convert data URIs into embedded images
      //e.g. "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
      if (preg_match('#^data:(image/(?:jpe?g|gif|png));?(base64)?,(.+)#', $url, $match)) {
        if (count($match) == 4 and static::ENCODING_BASE64 == $match [2]) {
          $data = base64_decode($match [3]);
        }
        elseif ('' == $match [2]) {
          $data = rawurldecode($match [3]);
        }
        else {
          //Not recognised so leave it alone
          continue;
        }
        //Hash the decoded data, not the URL so that the same data-URI image used in multiple places
        //will only be embedded once, even if it used a different encoding
        $cid = hash('sha256', $data) . '@phpmailer.0'; // RFC2392 S 2

        if (!$this->cidExists($cid)) {
          $this->addStringEmbeddedImage($data, $cid, 'embed' . $imgindex, static::ENCODING_BASE64, $match [1]);
        }
        $message = str_replace(
        $images [0][$imgindex], 
        $images [1][$imgindex] . '="cid:' . $cid . '"', 
        $message
        );
        continue;
      }
      if (// Only process relative URLs if a basedir is provided (i.e. no absolute local paths)
      !empty($basedir)
        // Ignore URLs containing parent dir traversal (..)
         and (strpos($url, '..') === false)
          // Do not change urls that are already inline images
           and 0 !== strpos($url, 'cid:')
          // Do not change absolute URLs, including anonymous protocol
           and !preg_match('#^[a-z][a-z0-9+.-]*:?//#i', $url)
        ) {
        $filename = static::mb_pathinfo($url, PATHINFO_BASENAME);
        $directory = dirname($url);
        if ('.' == $directory) {
          $directory = '';
        }
        $cid = hash('sha256', $url) . '@phpmailer.0'; // RFC2392 S 2
        if (strlen($basedir) > 1 and '/' != substr($basedir, -1)) {
          $basedir .= '/';
        }
        if (strlen($directory) > 1 and '/' != substr($directory, -1)) {
          $directory .= '/';
        }
        if ($this->addEmbeddedImage(
        $basedir . $directory . $filename, 
        $cid, 
        $filename, 
        static::ENCODING_BASE64, 
        static::_mime_types((string) static::mb_pathinfo($filename, PATHINFO_EXTENSION))
        )
        ) {
          $message = preg_replace(
          '/' . $images [1][$imgindex] . '=["\']' . preg_quote($url, '/') . '["\']/Ui', 
          $images [1][$imgindex] . '="cid:' . $cid . '"', 
          $message
          );
        }
      }
    }
  }
  $this->isHTML(true);
  // Convert all message body line breaks to LE, makes quoted-printable encoding work much better
  $this->Body = static::normalizeBreaks($message);
  $this->AltBody = static::normalizeBreaks($this->html2text($message, $advanced));
  if (!$this->alternativeExists()) {
    $this->AltBody = 'This is an HTML-only message. To view it, activate HTML in your email application.'
      . static::$LE;
  }

  return $this->Body;
}