Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions system/Email/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -705,13 +705,23 @@ 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';

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

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;
Expand Down
51 changes: 51 additions & 0 deletions tests/system/Email/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,55 @@ private function createMockEmail(): MockEmail

return new MockEmail($config);
}

public function testSetAttachmentCIDFile(): void
{
$email = $this->createMockEmail();

$email->setFrom('[email protected]', 'Your Name');
$email->setTo('[email protected]');

$filename = SUPPORTPATH . 'Images/ci-logo.png';
$email->attach($filename);
$cid = $email->setAttachmentCID($filename);
$email->setMessage('<img src="cid:' . $cid . '" alt="CI Logo">');

$this->assertTrue($email->send());

$this->assertStringStartsWith('ci-logo.png@', $cid);
$this->assertStringStartsWith(
'ci-logo.png@',
$email->archive['attachments'][0]['cid']
);
$this->assertMatchesRegularExpression(
'/<img src="cid:ci-logo.png@(.+?)" alt="CI Logo">/u',
$email->archive['body']
);
}

public function testSetAttachmentCIDBufferString(): void
{
$email = $this->createMockEmail();

$email->setFrom('[email protected]', 'Your Name');
$email->setTo('[email protected]');

$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('<img src="cid:' . $cid . '" alt="CI Logo">');

$this->assertTrue($email->send());

$this->assertStringStartsWith('image001.png@', $cid);
$this->assertStringStartsWith(
'image001.png@',
$email->archive['attachments'][0]['cid']
);
$this->assertMatchesRegularExpression(
'/<img src="cid:image001.png@(.+?)" alt="CI Logo">/u',
$email->archive['body']
);
}
}