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); } /** 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; } /**