Fix with mail

This commit is contained in:
Anna-Sara Sélea 2026-09-01 08:11:52 +02:00
parent 0e57494e0b
commit 9e275aeeae
4 changed files with 69 additions and 9 deletions

View file

@ -32,10 +32,12 @@ use Filament\Support\Enums\TextSize;
use Filament\Tables\Columns\Layout\Grid; use Filament\Tables\Columns\Layout\Grid;
use Filament\Notifications\Notification; use Filament\Notifications\Notification;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail; use Illuminate\Support\Facades\Mail;
use App\Mail\ReservationCreatedUser; use App\Mail\ReservationCreatedUser;
use App\Mail\ReservationCreated; use App\Mail\ReservationCreated;
use Filament\Forms\Components\Checkbox; use Filament\Forms\Components\Checkbox;
use Throwable;
class Reserve extends BasePage implements HasTable class Reserve extends BasePage implements HasTable
@ -202,13 +204,41 @@ class Reserve extends BasePage implements HasTable
'phone' => $data['phone'] 'phone' => $data['phone']
]); ]);
Item::where('id', $record->id)->update(['reserved' => true]); Item::where('id', $record->id)->update(['reserved' => true]);
Mail::to($data['email'])
->send(new ReservationCreatedUser($reservation)); // The reservation is already saved, so a mail failure must never
Mail::to(env('MAIL_TO_ADDRESS')) // take down the response for the visitor.
->send(new ReservationCreated($reservation)); $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() Notification::make()
->title(__('The game is reserved!')) ->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() ->success()
->seconds(10) ->seconds(10)
->send(); ->send();

View file

@ -27,9 +27,11 @@ use Filament\Support\Enums\IconPosition;
use Filament\Infolists\Components\TextEntry; use Filament\Infolists\Components\TextEntry;
use Filament\Infolists\Components\ImageEntry; use Filament\Infolists\Components\ImageEntry;
use Filament\Schemas\Components\Section; use Filament\Schemas\Components\Section;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail; use Illuminate\Support\Facades\Mail;
use App\Mail\ReservationCreatedUser; use App\Mail\ReservationCreatedUser;
use App\Mail\ReservationCreated; use App\Mail\ReservationCreated;
use Throwable;
use Filament\Forms\Components\FileUpload; use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\DatePicker; use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Toggle; use Filament\Forms\Components\Toggle;
@ -353,10 +355,24 @@ class ItemResource extends Resource
'phone' => $data['phone'] 'phone' => $data['phone']
]); ]);
Item::where('id', $record->id)->update(['reserved' => true]); Item::where('id', $record->id)->update(['reserved' => true]);
Mail::to($data['email'])
->send(new ReservationCreatedUser($reservation)); try {
Mail::to(env('MAIL_TO_ADDRESS')) Mail::to($data['email'])
->send(new ReservationCreated($reservation)); ->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) ->hidden(fn ($record) => $record->reserved)
]) ])

View file

@ -113,4 +113,17 @@ return [
'name' => env('MAIL_FROM_NAME', 'Example'), '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'),
]; ];

View file

@ -62,5 +62,6 @@
"User": "Användare", "User": "Användare",
"Username": "Namn", "Username": "Namn",
"Users": "Användare", "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." "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."
} }