-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
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
conrallendale
wants to merge
1
commit into
PHPMailer:master
Choose a base branch
from
conrallendale:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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) | ||
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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 . '"', | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 :)