diff --git a/system/Email/Email.php b/system/Email/Email.php index 3e4cd5671a97..6db52f3fa656 100644 --- a/system/Email/Email.php +++ b/system/Email/Email.php @@ -1308,7 +1308,7 @@ protected function appendAttachments(&$body, $boundary, $multipart = null) . 'Content-Type: ' . $attachment['type'] . '; name="' . $name . '"' . $this->newline . 'Content-Disposition: ' . $attachment['disposition'] . ';' . $this->newline . 'Content-Transfer-Encoding: base64' . $this->newline - . ($attachment['cid'] === '' ? '' : 'Content-ID: <' . $attachment['cid'] . '>' . $this->newline) + . (isset($attachment['cid']) && $attachment['cid'] !== '' ? 'Content-ID: <' . $attachment['cid'] . '>' . $this->newline : '') . $this->newline . $attachment['content'] . $this->newline; } diff --git a/tests/system/Email/EmailTest.php b/tests/system/Email/EmailTest.php index 8d865b58ddeb..ca42613505e4 100644 --- a/tests/system/Email/EmailTest.php +++ b/tests/system/Email/EmailTest.php @@ -21,6 +21,7 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; use ReflectionException; +use ReflectionMethod; /** * @internal @@ -220,6 +221,41 @@ public function testSetAttachmentCIDBufferString(): void ); } + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/9644 + * + * @throws ReflectionException + */ + public function testAppendAttachmentsWithoutCID(): void + { + $email = $this->createMockEmail(); + + // Manually inject an attachment without 'cid' + $this->setPrivateProperty($email, 'attachments', [ + [ + 'multipart' => 'mixed', + 'content' => 'VGhpcyBpcyBhIHRlc3QgZmlsZSBjb250ZW50Lg==', // base64 for "This is a test file content." + 'filename' => '', + 'type' => 'application/pdf', + 'name' => ['testfile.pdf'], + 'disposition' => 'attachment', + ], + ]); + + $body = ''; + $boundary = 'test-boundary'; + + // Use ReflectionMethod to call protected method with pass-by-reference + $refMethod = new ReflectionMethod($email, 'appendAttachments'); + $refMethod->invokeArgs($email, [&$body, $boundary, 'mixed']); + + // Assertion: Should not include a Content-ID header + $this->assertStringContainsString('Content-Type: application/pdf; name="testfile.pdf"', $body); + $this->assertStringContainsString('Content-Disposition: attachment;', $body); + $this->assertStringNotContainsString('Content-ID:', $body); + $this->assertStringContainsString('--' . $boundary . '--', $body); + } + /** * @throws ReflectionException */ diff --git a/user_guide_src/source/changelogs/v4.6.3.rst b/user_guide_src/source/changelogs/v4.6.3.rst index 6b44883af986..fa7786891956 100644 --- a/user_guide_src/source/changelogs/v4.6.3.rst +++ b/user_guide_src/source/changelogs/v4.6.3.rst @@ -30,6 +30,8 @@ Deprecations Bugs Fixed ********** +- **Email:** Fixed a bug with CID check when building email attachments. + See the repo's `CHANGELOG.md `_ for a complete list of bugs fixed.