PHPMailer\PHPMailer

PHPMailer::msgHTML()publicWP 1.0

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.

Method of the class: PHPMailer{}

No Hooks.

Return

String. The transformed message body

Usage

$PHPMailer = new PHPMailer();
$PHPMailer->msgHTML( $message, $basedir, $advanced );
$message(string) (required)
HTML message string
$basedir(string)
Absolute path to a base directory to prepend to relative paths to images
Default: ''
$advanced(true|false|callable)
Whether to use the internal HTML to text converter or your own custom converter
Default: false

Notes

  • See: PHPMailer::html2text()

PHPMailer::msgHTML() code WP 6.5.2

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=="
            $match = [];
            if (preg_match('#^data:(image/(?:jpe?g|gif|png));?(base64)?,(.+)#', $url, $match)) {
                if (count($match) === 4 && 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 = substr(hash('sha256', $data), 0, 32) . '@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 (..)
                && (strpos($url, '..') === false)
                //Do not change urls that are already inline images
                && 0 !== strpos($url, 'cid:')
                //Do not change absolute URLs, including anonymous protocol
                && !preg_match('#^[a-z][a-z0-9+.-]*:?//#i', $url)
            ) {
                $filename = static::mb_pathinfo($url, PATHINFO_BASENAME);
                $directory = dirname($url);
                if ('.' === $directory) {
                    $directory = '';
                }
                //RFC2392 S 2
                $cid = substr(hash('sha256', $url), 0, 32) . '@phpmailer.0';
                if (strlen($basedir) > 1 && '/' !== substr($basedir, -1)) {
                    $basedir .= '/';
                }
                if (strlen($directory) > 1 && '/' !== 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();
    //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;
}