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;
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('
');
+
+ $this->assertTrue($email->send());
+
+ $this->assertStringStartsWith('ci-logo.png@', $cid);
+ $this->assertStringStartsWith(
+ 'ci-logo.png@',
+ $email->archive['attachments'][0]['cid']
+ );
+ $this->assertMatchesRegularExpression(
+ '/
/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('
');
+
+ $this->assertTrue($email->send());
+
+ $this->assertStringStartsWith('image001.png@', $cid);
+ $this->assertStringStartsWith(
+ 'image001.png@',
+ $email->archive['attachments'][0]['cid']
+ );
+ $this->assertMatchesRegularExpression(
+ '/
/u',
+ $email->archive['body']
+ );
+ }
}