Skip to content

Commit

Permalink
fix: close smtp connection after sending or on error
Browse files Browse the repository at this point in the history
Unfortunately, `getSMTPObject()` creates a connection if it's not
already established. Ideally, the connection is already established due
to the send command some lines above so we only close it. There is no
way around it because the wrapped SMTP object is private.

Signed-off-by: Richard Steinmetz <[email protected]>
  • Loading branch information
st3iny committed Apr 15, 2024
1 parent 53d2517 commit 8f9a89b
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/Service/MailTransmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Horde_Imap_Client_Fetch_Query;
use Horde_Imap_Client_Ids;
use Horde_Mail_Transport_Null;
use Horde_Mail_Transport_Smtphorde;
use Horde_Mime_Exception;
use Horde_Mime_Headers;
use Horde_Mime_Headers_Addresses;
Expand Down Expand Up @@ -145,6 +146,14 @@ public function sendMessage(Account $account, LocalMessage $localMessage): void
$localMessage->setStatus(LocalMessage::STATUS_SMPT_SEND_FAIL);
$this->logger->error($e->getMessage(), ['exception' => $e]);
return;
} finally {
if ($transport instanceof Horde_Mail_Transport_Smtphorde) {
try {
$transport->getSMTPObject()->logout();
} catch (\Throwable $e) {
// Handle silently as this is a resource usage optimization
}
}
}

$this->eventDispatcher->dispatchTyped(
Expand Down

0 comments on commit 8f9a89b

Please sign in to comment.