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

[5.1] HTML Mail template images #42448

Merged
merged 6 commits into from Jan 31, 2024
Merged
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
21 changes: 15 additions & 6 deletions libraries/src/Mail/MailHelper.php
Expand Up @@ -216,16 +216,25 @@ public static function convertRelativeToAbsoluteUrls($content)
self::checkContent($content);
}

// Replace relative links, image sources with absolute Urls
// Replace relative links, image sources with absolute Urls and lazyloading
$protocols = '[a-zA-Z0-9\-]+:';
$attributes = ['href=', 'src=', 'poster='];
$attributes = ['href=', 'src=', 'poster=', 'loading=', 'data-path='];

foreach ($attributes as $attribute) {
if (strpos($content, $attribute) !== false) {
$regex = '#\s' . $attribute . '"(?!/|' . $protocols . '|\#|\')([^"]*)"#m';

$content = preg_replace($regex, ' ' . $attribute . '"' . $siteUrl . '$1"', $content);

// If the attribute is 'loading=', remove loading="lazy"
if ($attribute === 'loading=') {
$content = preg_replace('/\s' . $attribute . '"lazy"/i', '', $content);
} elseif ($attribute === 'data-path=') {
// If the attribute is 'data-path=', remove the entire attribute
$content = preg_replace('/\s' . $attribute . '"([^"]*)"/i', '', $content);
} else {
// Define a regular expression pattern for matching relative URLs in the specified attribute
$regex = '#\s' . $attribute . '"(?!/|' . $protocols . '|\#|\')([^"]*)"#m';

// Replace relative URLs with absolute URLs using the siteUrl variable
$content = preg_replace($regex, ' ' . $attribute . '"' . $siteUrl . '$1"', $content);
}
self::checkContent($content);
}
}
Expand Down