From 9e275aeeaee885cdc79ffa081e130227306cd861 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anna-Sara=20S=C3=A9lea?= Date: Tue, 1 Sep 2026 08:11:52 +0200 Subject: [PATCH] Fix with mail --- app/Filament/Pages/Reserve.php | 40 +++++++++++++++++++++---- app/Filament/Resources/ItemResource.php | 24 ++++++++++++--- config/mail.php | 13 ++++++++ lang/sv.json | 1 + 4 files changed, 69 insertions(+), 9 deletions(-) diff --git a/app/Filament/Pages/Reserve.php b/app/Filament/Pages/Reserve.php index e392d86..fc655bf 100644 --- a/app/Filament/Pages/Reserve.php +++ b/app/Filament/Pages/Reserve.php @@ -32,10 +32,12 @@ use Filament\Support\Enums\TextSize; use Filament\Tables\Columns\Layout\Grid; use Filament\Notifications\Notification; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Mail; use App\Mail\ReservationCreatedUser; use App\Mail\ReservationCreated; use Filament\Forms\Components\Checkbox; +use Throwable; class Reserve extends BasePage implements HasTable @@ -202,13 +204,41 @@ class Reserve extends BasePage implements HasTable 'phone' => $data['phone'] ]); Item::where('id', $record->id)->update(['reserved' => true]); - Mail::to($data['email']) - ->send(new ReservationCreatedUser($reservation)); - Mail::to(env('MAIL_TO_ADDRESS')) - ->send(new ReservationCreated($reservation)); + + // The reservation is already saved, so a mail failure must never + // take down the response for the visitor. + $mailSent = true; + + try { + Mail::to($data['email']) + ->send(new ReservationCreatedUser($reservation)); + } catch (Throwable $exception) { + $mailSent = false; + Log::error('Could not send reservation email to the visitor: ' . $exception->getMessage(), [ + 'reservation_id' => $reservation->id, + ]); + } + + if ($adminAddress = config('mail.to_address')) { + try { + Mail::to($adminAddress) + ->send(new ReservationCreated($reservation)); + } catch (Throwable $exception) { + Log::error('Could not send reservation email to staff: ' . $exception->getMessage(), [ + 'reservation_id' => $reservation->id, + ]); + } + } else { + Log::warning('mail.to_address is not set, no reservation email sent to staff.', [ + 'reservation_id' => $reservation->id, + ]); + } + Notification::make() ->title(__('The game is reserved!')) - ->body(__('A confirmation email has been sent to the address you provided. Read it for more information about collecting the game.')) + ->body($mailSent + ? __('A confirmation email has been sent to the address you provided. Read it for more information about collecting the game.') + : __('We could not send a confirmation email, but your reservation is registered and we will contact you.')) ->success() ->seconds(10) ->send(); diff --git a/app/Filament/Resources/ItemResource.php b/app/Filament/Resources/ItemResource.php index 29cdb0d..049a064 100644 --- a/app/Filament/Resources/ItemResource.php +++ b/app/Filament/Resources/ItemResource.php @@ -27,9 +27,11 @@ use Filament\Support\Enums\IconPosition; use Filament\Infolists\Components\TextEntry; use Filament\Infolists\Components\ImageEntry; use Filament\Schemas\Components\Section; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Mail; use App\Mail\ReservationCreatedUser; use App\Mail\ReservationCreated; +use Throwable; use Filament\Forms\Components\FileUpload; use Filament\Forms\Components\DatePicker; use Filament\Forms\Components\Toggle; @@ -353,10 +355,24 @@ class ItemResource extends Resource 'phone' => $data['phone'] ]); Item::where('id', $record->id)->update(['reserved' => true]); - Mail::to($data['email']) - ->send(new ReservationCreatedUser($reservation)); - Mail::to(env('MAIL_TO_ADDRESS')) - ->send(new ReservationCreated($reservation)); + + try { + Mail::to($data['email']) + ->send(new ReservationCreatedUser($reservation)); + + if ($adminAddress = config('mail.to_address')) { + Mail::to($adminAddress) + ->send(new ReservationCreated($reservation)); + } else { + Log::warning('mail.to_address is not set, no reservation email sent to staff.', [ + 'reservation_id' => $reservation->id, + ]); + } + } catch (Throwable $exception) { + Log::error('Could not send reservation email: ' . $exception->getMessage(), [ + 'reservation_id' => $reservation->id, + ]); + } }) ->hidden(fn ($record) => $record->reserved) ]) diff --git a/config/mail.php b/config/mail.php index 756305b..71c07b3 100644 --- a/config/mail.php +++ b/config/mail.php @@ -113,4 +113,17 @@ return [ 'name' => env('MAIL_FROM_NAME', 'Example'), ], + /* + |-------------------------------------------------------------------------- + | Internal "To" Address + |-------------------------------------------------------------------------- + | + | Address that receives the internal notices, such as a new reservation. + | Read it through config() and never through env(), so that it survives + | "php artisan config:cache". + | + */ + + 'to_address' => env('MAIL_TO_ADDRESS'), + ]; diff --git a/lang/sv.json b/lang/sv.json index 739d5ab..9044803 100644 --- a/lang/sv.json +++ b/lang/sv.json @@ -62,5 +62,6 @@ "User": "Användare", "Username": "Namn", "Users": "Användare", + "We could not send a confirmation email, but your reservation is registered and we will contact you.": "Vi kunde inte skicka något bekräftelsemail, men din reservation är registrerad och vi hör av oss.", "Welcome! Here you can reserve games that you would like to borrow.": "Välkommen! Här kan du reservera spel som du gärna vill låna." }