diff --git a/src/Controller/Mail/MailTestConnectionController.php b/src/Controller/Mail/MailTestConnectionController.php index eedee6a..6d25518 100644 --- a/src/Controller/Mail/MailTestConnectionController.php +++ b/src/Controller/Mail/MailTestConnectionController.php @@ -23,21 +23,23 @@ class MailTestConnectionController extends AbstractController public function __invoke(): JsonResponse { try { - $folders = $this->mailProvider->listFolders(); + $foldersCount = $this->mailProvider->testConnection(); return $this->json([ 'ok' => true, - 'foldersCount' => count($folders), + 'foldersCount' => $foldersCount, ]); - } catch (MailProviderException) { + } catch (MailProviderException $e) { return $this->json([ - 'ok' => false, - 'error' => 'Connexion IMAP impossible. Vérifiez la configuration.', + 'ok' => false, + 'error' => 'Connexion IMAP impossible. Vérifiez la configuration.', + 'detail' => $e->getMessage(), ]); - } catch (Throwable) { + } catch (Throwable $e) { return $this->json([ - 'ok' => false, - 'error' => 'Erreur inattendue lors du test de connexion.', + 'ok' => false, + 'error' => 'Erreur inattendue lors du test de connexion.', + 'detail' => $e->getMessage(), ]); } } diff --git a/src/Mail/ImapMailProvider.php b/src/Mail/ImapMailProvider.php index 1354d1f..eac2021 100644 --- a/src/Mail/ImapMailProvider.php +++ b/src/Mail/ImapMailProvider.php @@ -26,6 +26,22 @@ final class ImapMailProvider implements MailProviderInterface private readonly LoggerInterface $logger, ) {} + public function testConnection(): int + { + $client = $this->getClient(requireEnabled: false); + + try { + $folders = $client->getFolders(false); + $client->disconnect(); + + return count($folders); + } catch (Throwable $e) { + $this->logger->error('ImapMailProvider::testConnection failed: '.$e->getMessage()); + + throw MailProviderException::connectionFailed($e->getMessage()); + } + } + public function listFolders(): array { $client = $this->getClient(); @@ -269,12 +285,16 @@ final class ImapMailProvider implements MailProviderInterface } } - private function getClient(): Client + private function getClient(bool $requireEnabled = true): Client { $config = $this->configRepository->findSingleton(); - if (null === $config || !$config->isEnabled()) { - throw MailProviderException::connectionFailed('Mail configuration is missing or disabled'); + if (null === $config) { + throw MailProviderException::connectionFailed('Mail configuration is missing'); + } + + if ($requireEnabled && !$config->isEnabled()) { + throw MailProviderException::connectionFailed('Mail configuration is disabled'); } if (null === $config->getEncryptedPassword()) { diff --git a/src/Mail/MailProviderInterface.php b/src/Mail/MailProviderInterface.php index 1de7da3..9392f85 100644 --- a/src/Mail/MailProviderInterface.php +++ b/src/Mail/MailProviderInterface.php @@ -11,6 +11,15 @@ use App\Mail\Exception\MailProviderException; interface MailProviderInterface { + /** + * Opens a connection using the stored configuration and returns the number + * of folders found. Used by the admin "test connection" endpoint, so it + * MUST work even when the configuration is not yet enabled. + * + * @throws MailProviderException + */ + public function testConnection(): int; + /** * Returns the full folder tree of the configured mailbox. *