From 1741648fe6d2ad0b78deeceec85c2ef7a337f4ae Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 24 Jan 2024 07:04:04 +0900 Subject: [PATCH 1/2] test: add tests for setAttachmentCID() --- tests/system/Email/EmailTest.php | 51 ++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tests/system/Email/EmailTest.php b/tests/system/Email/EmailTest.php index dfcbedf183cc..77f40dead168 100644 --- a/tests/system/Email/EmailTest.php +++ b/tests/system/Email/EmailTest.php @@ -162,4 +162,55 @@ private function createMockEmail(): MockEmail return new MockEmail($config); } + + public function testSetAttachmentCIDFile(): void + { + $email = $this->createMockEmail(); + + $email->setFrom('your@example.com', 'Your Name'); + $email->setTo('foo@example.jp'); + + $filename = SUPPORTPATH . 'Images/ci-logo.png'; + $email->attach($filename); + $cid = $email->setAttachmentCID($filename); + $email->setMessage('CI Logo'); + + $this->assertTrue($email->send()); + + $this->assertStringStartsWith('ci-logo.png@', $cid); + $this->assertStringStartsWith( + 'ci-logo.png@', + $email->archive['attachments'][0]['cid'] + ); + $this->assertMatchesRegularExpression( + '/CI Logo/u', + $email->archive['body'] + ); + } + + public function testSetAttachmentCIDBufferString(): void + { + $email = $this->createMockEmail(); + + $email->setFrom('your@example.com', 'Your Name'); + $email->setTo('foo@example.jp'); + + $filename = SUPPORTPATH . 'Images/ci-logo.png'; + $imageData = file_get_contents($filename); + $email->attach($imageData, 'inline', 'image001.png', 'image/png'); + $cid = $email->setAttachmentCID('image001.png'); + $email->setMessage('CI Logo'); + + $this->assertTrue($email->send()); + + $this->assertStringStartsWith('image001.png@', $cid); + $this->assertStringStartsWith( + 'image001.png@', + $email->archive['attachments'][0]['cid'] + ); + $this->assertMatchesRegularExpression( + '/CI Logo/u', + $email->archive['body'] + ); + } } From 51d6963588c5c0c3761e8e2e26621c54d3c45e34 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 24 Jan 2024 07:07:04 +0900 Subject: [PATCH 2/2] fix: setAttachmentCID() does not work with buffer string --- system/Email/Email.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/system/Email/Email.php b/system/Email/Email.php index d18b40ff7cef..42835c3d404e 100644 --- a/system/Email/Email.php +++ b/system/Email/Email.php @@ -705,6 +705,7 @@ public function attach($file, $disposition = '', $newname = null, $mime = '') public function setAttachmentCID($filename) { foreach ($this->attachments as $i => $attachment) { + // For file path. if ($attachment['name'][0] === $filename) { $this->attachments[$i]['multipart'] = 'related'; @@ -712,6 +713,15 @@ public function setAttachmentCID($filename) return $this->attachments[$i]['cid']; } + + // For buffer string. + if ($attachment['name'][1] === $filename) { + $this->attachments[$i]['multipart'] = 'related'; + + $this->attachments[$i]['cid'] = uniqid(basename($attachment['name'][1]) . '@', true); + + return $this->attachments[$i]['cid']; + } } return false;