PHPMailer::msgHTML PHP Method

msgHTML() public method

Automatically inlines images and creates a plain-text version by converting the HTML, overwriting any existing values in Body and AltBody. $basedir is used when handling relative image paths, e.g. will look for an image file in $basedir/images/a.png and convert it to inline. If you don't want to apply these transformations to your HTML, just set Body and AltBody yourself.
public msgHTML ( string $message, string $basedir = '', boolean | callable $advanced = false ) : string
$message string HTML message string
$basedir string base directory for relative paths to images
$advanced boolean | callable Whether to use the internal HTML to text converter or your own custom converter @see PHPMailer::html2text()
return string $message The transformed message Body
    public function msgHTML($message, $basedir = '', $advanced = false)
    {
        preg_match_all('/(src|background)=["\'](.*)["\']/Ui', $message, $images);
        if (array_key_exists(2, $images)) {
            foreach ($images[2] as $imgindex => $url) {
                // Convert data URIs into embedded images
                if (preg_match('#^data:(image[^;,]*)(;base64)?,#', $url, $match)) {
                    $data = substr($url, strpos($url, ','));
                    if ($match[2]) {
                        $data = base64_decode($data);
                    } else {
                        $data = rawurldecode($data);
                    }
                    $cid = md5($url) . '@phpmailer.0';
                    // RFC2392 S 2
                    if ($this->addStringEmbeddedImage($data, $cid, 'embed' . $imgindex, 'base64', $match[1])) {
                        $message = str_replace($images[0][$imgindex], $images[1][$imgindex] . '="cid:' . $cid . '"', $message);
                    }
                } elseif (substr($url, 0, 4) !== 'cid:' && !preg_match('#^[a-z][a-z0-9+.-]*://#i', $url)) {
                    // Do not change urls for absolute images (thanks to corvuscorax)
                    // Do not change urls that are already inline images
                    $filename = basename($url);
                    $directory = dirname($url);
                    if ($directory == '.') {
                        $directory = '';
                    }
                    $cid = md5($url) . '@phpmailer.0';
                    // RFC2392 S 2
                    if (strlen($basedir) > 1 && substr($basedir, -1) != '/') {
                        $basedir .= '/';
                    }
                    if (strlen($directory) > 1 && substr($directory, -1) != '/') {
                        $directory .= '/';
                    }
                    if ($this->addEmbeddedImage($basedir . $directory . $filename, $cid, $filename, 'base64', self::_mime_types((string) self::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 CRLF, makes quoted-printable encoding work much better
        $this->Body = $this->normalizeBreaks($message);
        $this->AltBody = $this->normalizeBreaks($this->html2text($message, $advanced));
        if (!$this->alternativeExists()) {
            $this->AltBody = 'To view this email message, open it in a program that understands HTML!' . self::CRLF . self::CRLF;
        }
        return $this->Body;
    }

Usage Example

        /**
         * Send email
         * @param 			array $to
         * @param 			array $from
         * @param 			string $subject
         * @param 			string $text
         * @param 			bool $use_template
         * @return 			bool
         */
        public function send($to, $from, $subject, $text, $use_template = true)
        {
            $this->client->setFrom($from[1], $from[0]);
            $this->client->addAddress($to[1], $to[0]);
            $this->client->Subject = $subject;
            if ($use_template) {
                $template = '
			<!DOCTYPE HTML>
			<html dir="rtl">
				<head>
					<meta charset="utf-8">
				</head>
				<body style="font-family: tahoma, sans-serif !important;">
					<div style="font: 13px tahoma,sans-serif !important;direction: rtl;background-color: #e8e8e8;">
						<div style="width: 70%;background-color: #fff;background-color: #fff; border-radius: 3px;margin: auto;position: relative;border-left: 1px solid #d9d9d9;border-right: 1px solid #d9d9d9;">
							<div style="top: 0;position: absolute;width: 100%; height: 4px;background: url( \'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAECAYAAAD8kH0gAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAGhJREFUeNpi/P///xkGKFh27TfDkiu/GQiBKC1WhhgdVoLqXkzsZHje30ZQnUR+OYNkYRVWORZkxy0DOo6JgGERpDhuYgcDAxN+EyVyS3E6DgSYkB3HQG3HEQo5Ao4DO3AwOw4EAAIMAMZJM9nl1EbWAAAAAElFTkSuQmCC\' ) repeat;"></div>
							<div style="padding: 22px 15px;">
								{TEXT}
							</div>
							<div style="bottom: 0;position: absolute;width: 100%; height: 4px;background: url( \'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAECAYAAAD8kH0gAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAGhJREFUeNpi/P///xkGKFh27TfDkiu/GQiBKC1WhhgdVoLqXkzsZHje30ZQnUR+OYNkYRVWORZkxy0DOo6JgGERpDhuYgcDAxN+EyVyS3E6DgSYkB3HQG3HEQo5Ao4DO3AwOw4EAAIMAMZJM9nl1EbWAAAAAElFTkSuQmCC\' ) repeat;"></div>
						</div>
					</div>
				</body>
			</html>
			';
                $msg = str_replace('{TEXT}', $text, $template);
            } else {
                $msg = $text;
            }
            $this->client->msgHTML($msg);
            return $this->client->send();
        }
All Usage Examples Of PHPMailer::msgHTML