Skip to content

Commit

Permalink
fix: Email SMTP may throw Uncaught ErrorException
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Jun 24, 2022
1 parent 346fb6d commit c4022bb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
8 changes: 7 additions & 1 deletion system/Email/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -2144,7 +2144,13 @@ protected function mimeTypes($ext = '')
public function __destruct()
{
if (is_resource($this->SMTPConnect)) {
$this->sendCommand('quit');
try {
$this->sendCommand('quit');
} catch (ErrorException $e) {
$protocol = $this->getProtocol();
$method = 'sendWith' . ucfirst($protocol);
log_message('error', 'Email: ' . $method . ' throwed ' . $e);
}
}
}

Expand Down
19 changes: 19 additions & 0 deletions tests/system/Email/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use CodeIgniter\Events\Events;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockEmail;
use ErrorException;

/**
* @internal
Expand Down Expand Up @@ -136,6 +137,24 @@ public function testFailureDoesNotTriggerEvent()
$this->assertNull($result);
}

public function testDestructDoesNotThrowException()
{
$email = $this->getMockBuilder(Email::class)
->disableOriginalConstructor()
->onlyMethods(['sendCommand'])
->getMock();
$email->method('sendCommand')
->willThrowException(new ErrorException('SMTP Error.'));

// Force resource to be injected into the property
$SMTPConnect = fopen(__FILE__, 'rb');
$this->setPrivateProperty($email, 'SMTPConnect', $SMTPConnect);

$email->__destruct();

$this->assertTrue(true);
}

private function createMockEmail(): MockEmail
{
$config = config('Email');
Expand Down

0 comments on commit c4022bb

Please sign in to comment.