From a77612bad1faa1b1635c043bf840cb0f0b9c1faa Mon Sep 17 00:00:00 2001 From: Julien Rosset Date: Tue, 21 Oct 2025 16:24:02 +0200 Subject: [PATCH] Fix file processing and optimize when the same file if multiple times --- src/PHPMailerEnhanced/PHPMailerEnhanced.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/PHPMailerEnhanced/PHPMailerEnhanced.php b/src/PHPMailerEnhanced/PHPMailerEnhanced.php index a1963e3..fa358b2 100644 --- a/src/PHPMailerEnhanced/PHPMailerEnhanced.php +++ b/src/PHPMailerEnhanced/PHPMailerEnhanced.php @@ -209,7 +209,7 @@ class PHPMailerEnhanced extends PHPMailer { $link = $match[0]; //region Check the link has no protocol (http, cid, etc.) or it's “file” - if (preg_match('#^(?[a-z][a-z\d+.-]*):(?.+)$#i', $link, $match) === 1) { + if (preg_match('#^(?:(?[a-z][a-z\d+.-]*):)?(?.+)$#i', $link, $match) === 1) { if (($match['protocol'] ?? '') !== 'file') { return $link; } @@ -229,11 +229,17 @@ class PHPMailerEnhanced extends PHPMailer { return $link; } } + $path = realpath($path); //endregion //region Associate a cid to the file - $cid = $this->cidGenerate(); - $embeddedImages[$cid] = $path; + if (array_key_exists($path, $embeddedImages)) { + $cid = $embeddedImages[$path]; + } + else { + $cid = $this->cidGenerate(); + $embeddedImages[$path] = $cid; + } //endregion return 'cid:' . $cid; @@ -251,7 +257,7 @@ class PHPMailerEnhanced extends PHPMailer { $this->$bodyProperty = $body; //endregion //region Embed the files in the mail - foreach ($embeddedImages as $cid => $path) { + foreach ($embeddedImages as $path => $cid) { $this->addEmbeddedImage($path, $cid); } //endregion