Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change msgHtml to allow callback as image resolver #1799

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 42 additions & 30 deletions src/PHPMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3856,20 +3856,21 @@ public function getCustomHeaders()
* 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.
*
* @param string $message HTML message string
* @param string $basedir Absolute path to a base directory to prepend to relative paths to images
* @param bool|callable $advanced Whether to use the internal HTML to text converter
* or your own custom converter @see PHPMailer::html2text()
* @param string $message HTML message string
* @param string|callable $imageResolver Absolute path to a base directory to prepend to relative paths to images
* or a custom resolver, returning a object with data, name and type of the image
* @param bool|callable $advanced 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)
public function msgHTML($message, $imageResolver = '', $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 .= '/';
if (is_string($imageResolver) && strlen($imageResolver) > 1 && '/' != substr($imageResolver, -1)) {
// Ensure $imageResolver has a trailing /
$imageResolver .= '/';
}
foreach ($images[2] as $imgindex => $url) {
// Convert data URIs into embedded images
Expand Down Expand Up @@ -3897,35 +3898,46 @@ public function msgHTML($message, $basedir = '', $advanced = false)
);
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)
if (// Ignore URLs containing parent dir traversal (..)
(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 .= '/';
$added = false;

// Only process relative URLs if a basedir is provided (i.e. no absolute local paths)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the var name in the comment :)

if (is_string($imageResolver) && !empty($imageResolver)) {
$filename = static::mb_pathinfo($url, PATHINFO_BASENAME);
$directory = dirname($url);
if ('.' == $directory) {
$directory = '';
}
if (strlen($directory) > 1 and '/' != substr($directory, -1)) {
$directory .= '/';
}
$added = $this->addEmbeddedImage(
$imageResolver . $directory . $filename,
$cid,
$filename,
static::ENCODING_BASE64,
static::_mime_types((string) static::mb_pathinfo($filename, PATHINFO_EXTENSION))
);
} elseif (is_callable($imageResolver)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the passed param is not callable we can thrown an exception telling about it. Otherwise it will be hard to debug what exactly is happening

$object = $imageResolver($url);

$added = $this->addStringEmbeddedImage(
$object['data'],
$cid,
$object['name'],
'base64',
$object['type']
);
}
if ($this->addEmbeddedImage(
$basedir . $directory . $filename,
$cid,
$filename,
static::ENCODING_BASE64,
static::_mime_types((string) static::mb_pathinfo($filename, PATHINFO_EXTENSION))
)
) {

if ($added) {
$message = preg_replace(
'/' . $images[1][$imgindex] . '=["\']' . preg_quote($url, '/') . '["\']/Ui',
$images[1][$imgindex] . '="cid:' . $cid . '"',
Expand Down