diff --git a/app/Channels/Mail/MailboxReader.php b/app/Channels/Mail/MailboxReader.php index e7988d8..57a700f 100644 --- a/app/Channels/Mail/MailboxReader.php +++ b/app/Channels/Mail/MailboxReader.php @@ -7,7 +7,9 @@ use Carbon\CarbonImmutable; use Illuminate\Support\Facades\Cache; use Throwable; +use Webklex\PHPIMAP\Client; use Webklex\PHPIMAP\ClientManager; +use Webklex\PHPIMAP\Folder; use Webklex\PHPIMAP\Message; /** @@ -70,7 +72,7 @@ public function fetch(array $senders): array 'protocol' => 'imap', ]); $client->connect(); - $folder = $client->getFolderByPath($config['folder']); + $folder = self::folder($client, $config['folder']); $mails = []; foreach ($senders as $sender) { @@ -119,7 +121,7 @@ public function fetchAll(): array 'username' => $config['username'], 'password' => $config['password'], 'protocol' => 'imap', ]); $client->connect(); - $messages = $client->getFolderByPath($config['folder'])->query() + $messages = self::folder($client, $config['folder'])->query() ->whereSince(CarbonImmutable::today()->subDays((int) config('fewo.mailbox.lookback_days', 3))) ->leaveUnread() ->get(); @@ -156,7 +158,7 @@ public function probe(array $config): int 'username' => $config['username'], 'password' => $config['password'], 'protocol' => 'imap', ]); $client->connect(); - $count = $client->getFolderByPath($config['folder'])->query() + $count = self::folder($client, $config['folder'])->query() ->whereSince(CarbonImmutable::today()->subDays((int) config('fewo.mailbox.lookback_days', 3))) ->leaveUnread()->setFetchBody(false)->count(); $client->disconnect(); @@ -167,6 +169,28 @@ public function probe(array $config): int return (int) $count; } + /** + * The folder to read. The inbox is called "INBOX" on the IMAP server, whatever the provider's web mail shows + * ("Posteingang", "Inbox"); other names match regardless of case. An unknown name names the existing folders. + * + * @throws ChannelSyncException + */ + public static function folder(Client $client, ?string $name): Folder + { + $name = trim((string) $name); + $wanted = $name === '' || in_array(mb_strtolower($name), ['inbox', 'posteingang'], true) ? 'INBOX' : $name; + + $folders = $client->getFolders(false); + $folder = $folders->first(fn (Folder $folder): bool => $folder->path === $wanted) + ?? $folders->first(fn (Folder $folder): bool => mb_strtolower($folder->path) === mb_strtolower($wanted) || mb_strtolower($folder->name) === mb_strtolower($wanted)); + + if ($folder === null) { + throw new ChannelSyncException('Ordner „'.$name.'“ gibt es in diesem Postfach nicht. Vorhanden: '.$folders->map(fn (Folder $folder): string => $folder->path)->implode(', ').' (der Posteingang heißt „INBOX“).'); + } + + return $folder; + } + private function toPortalMail(Message $message): PortalMail { $text = $message->getTextBody(); diff --git a/app/Filament/Pages/Operations.php b/app/Filament/Pages/Operations.php index 99f30cb..570c851 100644 --- a/app/Filament/Pages/Operations.php +++ b/app/Filament/Pages/Operations.php @@ -254,8 +254,9 @@ public function form(Schema $schema): Schema Select::make('mailbox_encryption') ->label('Verschlüsselung') ->options(['ssl' => 'SSL', 'tls' => 'TLS', 'starttls' => 'STARTTLS']) - ->placeholder('SSL'), - TextInput::make('mailbox_folder')->label('Ordner')->placeholder('INBOX')->maxLength(190), + ->placeholder('Standard (SSL)'), + TextInput::make('mailbox_folder')->label('Ordner')->placeholder('INBOX')->maxLength(190) + ->helperText('Leer lassen für den Posteingang (auf dem Server „INBOX“, auch „Posteingang“ wird erkannt).'), Select::make('mailbox_mode')->label('Was wird gelesen?') ->options([Mailbox::PORTAL_ONLY => 'Nur Buchungsmails der Portale (wie bisher)', Mailbox::FULL => 'Alle Nachrichten (eigenes Postfach für die Gästekommunikation)']) ->required() diff --git a/tests/Feature/GuestMailInboxTest.php b/tests/Feature/GuestMailInboxTest.php index d3c89c7..d46c7bf 100644 --- a/tests/Feature/GuestMailInboxTest.php +++ b/tests/Feature/GuestMailInboxTest.php @@ -35,6 +35,9 @@ use Livewire\Livewire; use Mockery; use Tests\TestCase; +use Webklex\PHPIMAP\Client; +use Webklex\PHPIMAP\Folder; +use Webklex\PHPIMAP\Support\FolderCollection; /** * v0.11 package U5: the inbox of the guest communication mailbox – stored once, portal notifications through the @@ -200,6 +203,27 @@ public function test_the_mailbox_can_be_checked_and_incoming_mails_show_on_the_c ->assertSee('Frage zu '.$booking->reference); } + public function test_the_inbox_is_found_under_its_web_mail_name_and_an_unknown_folder_is_explained(): void + { + $folder = function (string $path): Folder { + $folder = Mockery::mock(Folder::class); + $folder->path = $path; + $folder->name = $path; + + return $folder; + }; + $client = Mockery::mock(Client::class); + $client->shouldReceive('getFolders')->andReturn(FolderCollection::make([$folder('Trash'), $folder('Sent Items'), $folder('INBOX')])); + + foreach (['Posteingang', '', 'inbox', 'INBOX'] as $name) { + $this->assertSame('INBOX', MailboxReader::folder($client, $name)->path); + } + $this->assertSame('Sent Items', MailboxReader::folder($client, 'sent items')->path); + + $this->expectExceptionMessage('Vorhanden: Trash, Sent Items, INBOX'); + MailboxReader::folder($client, 'Archiv'); + } + private function request(string $arrival = '2026-11-07', string $departure = '2026-11-10'): Booking { return app(CreateReservation::class)->handle($this->property->fresh(), new StayRequest(CarbonImmutable::parse($arrival), CarbonImmutable::parse($departure), 2), ['first_name' => 'Gesa', 'last_name' => 'Gast', 'email' => 'gesa@example.org'])->fresh();