From 71ed073b3cf7cebbb25d86efb18ade5f16b938d3 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Tue, 17 Jun 2025 12:25:33 -0400 Subject: [PATCH 1/2] Prevent exceptions or warnings upon destruct --- src/Connection/ImapConnection.php | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/Connection/ImapConnection.php b/src/Connection/ImapConnection.php index 21e1975..e41484d 100644 --- a/src/Connection/ImapConnection.php +++ b/src/Connection/ImapConnection.php @@ -14,7 +14,6 @@ use DirectoryTree\ImapEngine\Connection\Streams\StreamInterface; use DirectoryTree\ImapEngine\Connection\Tokens\Token; use DirectoryTree\ImapEngine\Enums\ImapFetchIdentifier; -use DirectoryTree\ImapEngine\Exceptions\Exception; use DirectoryTree\ImapEngine\Exceptions\ImapCommandException; use DirectoryTree\ImapEngine\Exceptions\ImapConnectionClosedException; use DirectoryTree\ImapEngine\Exceptions\ImapConnectionFailedException; @@ -22,6 +21,7 @@ use DirectoryTree\ImapEngine\Exceptions\ImapResponseException; use DirectoryTree\ImapEngine\Exceptions\ImapStreamException; use DirectoryTree\ImapEngine\Support\Str; +use Exception; use Generator; use LogicException; @@ -69,8 +69,14 @@ public static function fake(array $responses = []): static */ public function __destruct() { - if ($this->connected()) { - $this->logout(); + if (! $this->connected()) { + return; + } + + try { + @$this->logout(); + } catch (Exception $e) { + // Do nothing. } } @@ -182,14 +188,7 @@ public function login(string $user, string $password): TaggedResponse */ public function logout(): void { - try { - // It's generally acceptable to send a logout command to an IMAP server - // and not wait for a response. If the server encounters an error - // processing the request, we will have to reconnect anyway. - $this->send('LOGOUT', tag: $tag); - } catch (Exception) { - // Do nothing. - } + $this->send('LOGOUT', tag: $tag); } /** From 0a199056d588ace470014aa5e9119a421e414119 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Tue, 17 Jun 2025 12:27:21 -0400 Subject: [PATCH 2/2] Disconnect gracefully --- src/Mailbox.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Mailbox.php b/src/Mailbox.php index 5adc0ba..f2884ea 100644 --- a/src/Mailbox.php +++ b/src/Mailbox.php @@ -8,6 +8,7 @@ use DirectoryTree\ImapEngine\Connection\Loggers\FileLogger; use DirectoryTree\ImapEngine\Connection\Streams\ImapStream; use DirectoryTree\ImapEngine\Connection\Tokens\Atom; +use Exception; class Mailbox implements MailboxInterface { @@ -167,12 +168,14 @@ protected function authenticate(): void */ public function disconnect(): void { - if ($this->connected()) { - $this->connection->logout(); - $this->connection->disconnect(); + try { + $this->connection?->logout(); + $this->connection?->disconnect(); + } catch (Exception) { + // Do nothing. + } finally { + $this->connection = null; } - - $this->connection = null; } /**