Merge pull request #3036 from mariuszkrzaczkowski/patch-1

Add cid default domain
This commit is contained in:
Marcus Bointon 2025-10-06 09:55:06 +02:00 committed by GitHub
commit 97ccd46848
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 42 additions and 6 deletions

View File

@ -1,5 +1,8 @@
# PHPMailer Change Log
## WIP
* Use From domain when generating CIDs in msgHTML
## Version 6.11.1 (September 30th, 2025)
* Avoid function signature problems with the deprecation of `$useimap` in `parseAddresses`.

View File

@ -4592,10 +4592,10 @@ class PHPMailer
* 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
* @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
* @return string The transformed message body
*
* @throws Exception
@ -4604,6 +4604,12 @@ class PHPMailer
*/
public function msgHTML($message, $basedir = '', $advanced = false)
{
$cid_domain = 'phpmailer.0';
if (filter_var($this->From, FILTER_VALIDATE_EMAIL)) {
//prepend with a character to create valid RFC822 string in order to validate
$cid_domain = substr($this->From, strrpos($this->From, '@') + 1);
}
preg_match_all('/(?<!-)(src|background)=["\'](.*)["\']/Ui', $message, $images);
if (array_key_exists(2, $images)) {
if (strlen($basedir) > 1 && '/' !== substr($basedir, -1)) {
@ -4625,7 +4631,7 @@ class PHPMailer
}
//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
$cid = substr(hash('sha256', $data), 0, 32) . '@' . $cid_domain; //RFC2392 S 2
if (!$this->cidExists($cid)) {
$this->addStringEmbeddedImage(
@ -4659,7 +4665,7 @@ class PHPMailer
$directory = '';
}
//RFC2392 S 2
$cid = substr(hash('sha256', $url), 0, 32) . '@phpmailer.0';
$cid = substr(hash('sha256', $url), 0, 32) . '@' . $cid_domain;
if (strlen($basedir) > 1 && '/' !== substr($basedir, -1)) {
$basedir .= '/';
}

View File

@ -597,6 +597,7 @@ EOT;
*/
public function testEmbeddedImage()
{
$this->Mail->From = '';
$this->Mail->msgHTML('<!DOCTYPE html>
<html lang="en">
<head>
@ -615,6 +616,32 @@ EOT;
);
}
/**
* An embedded attachment test with custom cid domain.
*/
public function testEmbeddedImageCustomCidDomain()
{
$result = $this->Mail->setFrom('test@example.com');
self::assertTrue($result, 'setFrom failed');
$this->Mail->msgHTML('<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>E-Mail Inline Image Test</title>
</head>
<body>
<p><img src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="></p>
</body>
</html>', '', false);
$this->Mail->preSend();
self::assertStringContainsString(
'Content-ID: <bb229a48bee31f5d54ca12dc9bd960c6@example.com>',
$this->Mail->getSentMIMEMessage(),
'Embedded image header encoding incorrect.'
);
}
/**
* An embedded attachment test.
*/