Skip to content

Commit 47643f9

Browse files
committed
fix: Email SMTP may throw Uncaught ErrorException
1 parent 346fb6d commit 47643f9

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

system/Email/Email.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2144,7 +2144,13 @@ protected function mimeTypes($ext = '')
21442144
public function __destruct()
21452145
{
21462146
if (is_resource($this->SMTPConnect)) {
2147-
$this->sendCommand('quit');
2147+
try {
2148+
$this->sendCommand('quit');
2149+
} catch (ErrorException $e) {
2150+
$protocol = $this->getProtocol();
2151+
$method = 'sendWith' . ucfirst($protocol);
2152+
log_message('error', 'Email: ' . $method . ' throwed ' . $e);
2153+
}
21482154
}
21492155
}
21502156

tests/system/Email/EmailTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use CodeIgniter\Events\Events;
1515
use CodeIgniter\Test\CIUnitTestCase;
1616
use CodeIgniter\Test\Mock\MockEmail;
17+
use ErrorException;
1718

1819
/**
1920
* @internal
@@ -136,6 +137,24 @@ public function testFailureDoesNotTriggerEvent()
136137
$this->assertNull($result);
137138
}
138139

140+
public function testDestructDoesNotThrowException()
141+
{
142+
$email = $this->getMockBuilder(Email::class)
143+
->disableOriginalConstructor()
144+
->onlyMethods(['sendCommand'])
145+
->getMock();
146+
$email->method('sendCommand')
147+
->will($this->throwException(new ErrorException('SMTP Error.')));
148+
149+
// Force resource to be injected into the property
150+
$SMTPConnect = fopen(__FILE__, 'rb');
151+
$this->setPrivateProperty($email, 'SMTPConnect', $SMTPConnect);
152+
153+
$email->__destruct();
154+
155+
$this->assertTrue(true);
156+
}
157+
139158
private function createMockEmail(): MockEmail
140159
{
141160
$config = config('Email');

0 commit comments

Comments
 (0)