Skip to content

Commit ca7d92c

Browse files
authored
Merge pull request #8446 from kenjis/fix-email-attach-cid
fix: [Email] setAttachmentCID() does not work with buffer string
2 parents 1545503 + 51d6963 commit ca7d92c

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

system/Email/Email.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,13 +705,23 @@ public function attach($file, $disposition = '', $newname = null, $mime = '')
705705
public function setAttachmentCID($filename)
706706
{
707707
foreach ($this->attachments as $i => $attachment) {
708+
// For file path.
708709
if ($attachment['name'][0] === $filename) {
709710
$this->attachments[$i]['multipart'] = 'related';
710711

711712
$this->attachments[$i]['cid'] = uniqid(basename($attachment['name'][0]) . '@', true);
712713

713714
return $this->attachments[$i]['cid'];
714715
}
716+
717+
// For buffer string.
718+
if ($attachment['name'][1] === $filename) {
719+
$this->attachments[$i]['multipart'] = 'related';
720+
721+
$this->attachments[$i]['cid'] = uniqid(basename($attachment['name'][1]) . '@', true);
722+
723+
return $this->attachments[$i]['cid'];
724+
}
715725
}
716726

717727
return false;

tests/system/Email/EmailTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,55 @@ private function createMockEmail(): MockEmail
162162

163163
return new MockEmail($config);
164164
}
165+
166+
public function testSetAttachmentCIDFile(): void
167+
{
168+
$email = $this->createMockEmail();
169+
170+
$email->setFrom('[email protected]', 'Your Name');
171+
$email->setTo('[email protected]');
172+
173+
$filename = SUPPORTPATH . 'Images/ci-logo.png';
174+
$email->attach($filename);
175+
$cid = $email->setAttachmentCID($filename);
176+
$email->setMessage('<img src="cid:' . $cid . '" alt="CI Logo">');
177+
178+
$this->assertTrue($email->send());
179+
180+
$this->assertStringStartsWith('ci-logo.png@', $cid);
181+
$this->assertStringStartsWith(
182+
'ci-logo.png@',
183+
$email->archive['attachments'][0]['cid']
184+
);
185+
$this->assertMatchesRegularExpression(
186+
'/<img src="cid:ci-logo.png@(.+?)" alt="CI Logo">/u',
187+
$email->archive['body']
188+
);
189+
}
190+
191+
public function testSetAttachmentCIDBufferString(): void
192+
{
193+
$email = $this->createMockEmail();
194+
195+
$email->setFrom('[email protected]', 'Your Name');
196+
$email->setTo('[email protected]');
197+
198+
$filename = SUPPORTPATH . 'Images/ci-logo.png';
199+
$imageData = file_get_contents($filename);
200+
$email->attach($imageData, 'inline', 'image001.png', 'image/png');
201+
$cid = $email->setAttachmentCID('image001.png');
202+
$email->setMessage('<img src="cid:' . $cid . '" alt="CI Logo">');
203+
204+
$this->assertTrue($email->send());
205+
206+
$this->assertStringStartsWith('image001.png@', $cid);
207+
$this->assertStringStartsWith(
208+
'image001.png@',
209+
$email->archive['attachments'][0]['cid']
210+
);
211+
$this->assertMatchesRegularExpression(
212+
'/<img src="cid:image001.png@(.+?)" alt="CI Logo">/u',
213+
$email->archive['body']
214+
);
215+
}
165216
}

0 commit comments

Comments
 (0)