Skip to content

Commit 3aeabec

Browse files
authored
Merge pull request #9645 from michalsn/fix/email-cid
fix: CID check in Email class
2 parents 627235c + c02184d commit 3aeabec

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

system/Email/Email.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1308,7 +1308,7 @@ protected function appendAttachments(&$body, $boundary, $multipart = null)
13081308
. 'Content-Type: ' . $attachment['type'] . '; name="' . $name . '"' . $this->newline
13091309
. 'Content-Disposition: ' . $attachment['disposition'] . ';' . $this->newline
13101310
. 'Content-Transfer-Encoding: base64' . $this->newline
1311-
. ($attachment['cid'] === '' ? '' : 'Content-ID: <' . $attachment['cid'] . '>' . $this->newline)
1311+
. (isset($attachment['cid']) && $attachment['cid'] !== '' ? 'Content-ID: <' . $attachment['cid'] . '>' . $this->newline : '')
13121312
. $this->newline
13131313
. $attachment['content'] . $this->newline;
13141314
}

tests/system/Email/EmailTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use PHPUnit\Framework\Attributes\DataProvider;
2222
use PHPUnit\Framework\Attributes\Group;
2323
use ReflectionException;
24+
use ReflectionMethod;
2425

2526
/**
2627
* @internal
@@ -220,6 +221,41 @@ public function testSetAttachmentCIDBufferString(): void
220221
);
221222
}
222223

224+
/**
225+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/9644
226+
*
227+
* @throws ReflectionException
228+
*/
229+
public function testAppendAttachmentsWithoutCID(): void
230+
{
231+
$email = $this->createMockEmail();
232+
233+
// Manually inject an attachment without 'cid'
234+
$this->setPrivateProperty($email, 'attachments', [
235+
[
236+
'multipart' => 'mixed',
237+
'content' => 'VGhpcyBpcyBhIHRlc3QgZmlsZSBjb250ZW50Lg==', // base64 for "This is a test file content."
238+
'filename' => '',
239+
'type' => 'application/pdf',
240+
'name' => ['testfile.pdf'],
241+
'disposition' => 'attachment',
242+
],
243+
]);
244+
245+
$body = '';
246+
$boundary = 'test-boundary';
247+
248+
// Use ReflectionMethod to call protected method with pass-by-reference
249+
$refMethod = new ReflectionMethod($email, 'appendAttachments');
250+
$refMethod->invokeArgs($email, [&$body, $boundary, 'mixed']);
251+
252+
// Assertion: Should not include a Content-ID header
253+
$this->assertStringContainsString('Content-Type: application/pdf; name="testfile.pdf"', $body);
254+
$this->assertStringContainsString('Content-Disposition: attachment;', $body);
255+
$this->assertStringNotContainsString('Content-ID:', $body);
256+
$this->assertStringContainsString('--' . $boundary . '--', $body);
257+
}
258+
223259
/**
224260
* @throws ReflectionException
225261
*/

user_guide_src/source/changelogs/v4.6.3.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Deprecations
3030
Bugs Fixed
3131
**********
3232

33+
- **Email:** Fixed a bug with CID check when building email attachments.
34+
3335
See the repo's
3436
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
3537
for a complete list of bugs fixed.

0 commit comments

Comments
 (0)