From 0bb6c031227b33e88f3829609502f33a0d44e658 Mon Sep 17 00:00:00 2001 From: Yam <2872995+Yam-s@users.noreply.github.com> Date: Mon, 25 May 2020 17:16:18 +0100 Subject: [PATCH] Add email error handling --- .../app/Listeners/SendAdminFileUploaded.php | 13 ++++++-- .../app/Listeners/SendAdminRemovedShift.php | 31 +++++++++++-------- laravel/app/Listeners/SendAdminWelcome.php | 13 ++++++-- laravel/app/Listeners/SendUserFileChanged.php | 26 ++++++++++------ laravel/app/Listeners/SendUserMessage.php | 13 ++++++-- .../Listeners/SendUserShiftConfirmation.php | 13 ++++++-- laravel/app/Listeners/SendUserWelcome.php | 13 ++++++-- laravel/resources/views/app.blade.php | 6 ++++ 8 files changed, 90 insertions(+), 38 deletions(-) diff --git a/laravel/app/Listeners/SendAdminFileUploaded.php b/laravel/app/Listeners/SendAdminFileUploaded.php index da8c84c7..79fc839a 100644 --- a/laravel/app/Listeners/SendAdminFileUploaded.php +++ b/laravel/app/Listeners/SendAdminFileUploaded.php @@ -34,9 +34,16 @@ public function handle(FileUploaded $event) // TODO: Options to choose which admins to notify $admin = User::where('id', 1)->first(); - Mail::send('emails/admin-file-uploaded', compact('file'), function ($message) use ($admin) + try { - $message->to($admin->email, $admin->name)->subject('New file uploaded!'); - }); + Mail::send('emails/admin-file-uploaded', compact('file'), function ($message) use ($admin) + { + $message->to($admin->email, $admin->name)->subject('New file uploaded!'); + }); + } + catch (\Exception $exception) + { + app('request')->session()->flash('warning', "Unable to send email notification, SMTP error. Please notify the administrator of this volunteer database."); + } } } diff --git a/laravel/app/Listeners/SendAdminRemovedShift.php b/laravel/app/Listeners/SendAdminRemovedShift.php index b488013c..fd7e3c2e 100644 --- a/laravel/app/Listeners/SendAdminRemovedShift.php +++ b/laravel/app/Listeners/SendAdminRemovedShift.php @@ -33,20 +33,25 @@ public function handle(SlotChanged $event) if($event->change['status'] === 'released' && $event->change['admin_released'] === true) { $schedule = $event->slot->schedule; + $slot = $event->slot; + $user_email = $event->slot->user->email; + $user_name = $event->slot->user->name; + $shift_name = $event->slot->schedule->shift->name; + $shift_date = Carbon::createFromFormat('Y-m-d', $schedule->start_date)->toFormattedDateString(); + $shift_time = $schedule->start_time; - $slot = $event->slot; - $user_email = $event->slot->user->email; - $user_name = $event->slot->user->name; - $shift_name = $event->slot->schedule->shift->name; - $shift_date = Carbon::createFromFormat('Y-m-d', $schedule->start_date)->toFormattedDateString(); - $shift_time = $schedule->start_time; - - $event_data = compact('slot', 'user_email', 'user_name', 'shift_name', 'shift_date', 'shift_time'); - - Mail::send('emails/admin-removed-shift', $event_data, function ($message) use ($user_email, $user_name) - { - $message->to($user_email, $user_name)->subject('Shift reschedule required!'); - }); + $event_data = compact('slot', 'user_email', 'user_name', 'shift_name', 'shift_date', 'shift_time'); + try + { + Mail::send('emails/admin-removed-shift', $event_data, function ($message) use ($user_email, $user_name) + { + $message->to($user_email, $user_name)->subject('Shift reschedule required!'); + }); + } + catch (\Exception $exception) + { + app('request')->session()->flash('warning', "Unable to send email notification, SMTP error. Please notify the administrator of this volunteer database."); + } } } } diff --git a/laravel/app/Listeners/SendAdminWelcome.php b/laravel/app/Listeners/SendAdminWelcome.php index ea84883c..77185893 100644 --- a/laravel/app/Listeners/SendAdminWelcome.php +++ b/laravel/app/Listeners/SendAdminWelcome.php @@ -34,9 +34,16 @@ public function handle(UserRegistered $event) // TODO: Options to choose which admins to notify $admin = User::where('id', 1)->first(); - Mail::send('emails/admin-welcome', compact('user'), function ($message) use ($admin) + try { - $message->to($admin->email, $admin->name)->subject('New user registered!'); - }); + Mail::send('emails/admin-welcome', compact('user'), function ($message) use ($admin) + { + $message->to($admin->email, $admin->name)->subject('New user registered!'); + }); + } + catch (\Exception $exception) + { + app('request')->session()->flash('warning', "Unable to send email confirmation, SMTP error. Please notify the administrator of this volunteer database."); + } } } diff --git a/laravel/app/Listeners/SendUserFileChanged.php b/laravel/app/Listeners/SendUserFileChanged.php index 1c2709e7..421ac63a 100644 --- a/laravel/app/Listeners/SendUserFileChanged.php +++ b/laravel/app/Listeners/SendUserFileChanged.php @@ -29,20 +29,26 @@ public function handle(FileChanged $event) { $file = $event->file; $user = $event->file->user; - - if($file->status == 'approved') + try { - Mail::send('emails/user-file-approved', compact('file', 'user'), function ($message) use ($user) + if($file->status == 'approved') + { + Mail::send('emails/user-file-approved', compact('file', 'user'), function ($message) use ($user) + { + $message->to($user->email, $user->name)->subject('Uploaded File Approved'); + }); + } + elseif($file->status == 'denied') { - $message->to($user->email, $user->name)->subject('Uploaded File Approved'); - }); + Mail::send('emails/user-file-denied', compact('file', 'user'), function ($message) use ($user) + { + $message->to($user->email, $user->name)->subject('Uploaded File Denied'); + }); + } } - elseif($file->status == 'denied') + catch (\Exception $exception) { - Mail::send('emails/user-file-denied', compact('file', 'user'), function ($message) use ($user) - { - $message->to($user->email, $user->name)->subject('Uploaded File Denied'); - }); + // Todo: Have a warning show up when the admin clicks save. } } } diff --git a/laravel/app/Listeners/SendUserMessage.php b/laravel/app/Listeners/SendUserMessage.php index a3c43180..16590c58 100644 --- a/laravel/app/Listeners/SendUserMessage.php +++ b/laravel/app/Listeners/SendUserMessage.php @@ -45,9 +45,16 @@ private function forgotPassword($event) { $user = $event->user; - Mail::send('emails/forgot-password', compact('user'), function ($message) use ($user) + try { - $message->to($user->email, $user->name)->subject('Your Password Reset Code'); - }); + Mail::send('emails/forgot-password', compact('user'), function ($message) use ($user) + { + $message->to($user->email, $user->name)->subject('Your Password Reset Code'); + }); + } + catch (\Exception $exception) + { + app('request')->session()->flash('error', "Unable to send email, SMTP error. Please notify the administrator of this volunteer database."); + } } } diff --git a/laravel/app/Listeners/SendUserShiftConfirmation.php b/laravel/app/Listeners/SendUserShiftConfirmation.php index ddc592ef..ddf0f9e3 100644 --- a/laravel/app/Listeners/SendUserShiftConfirmation.php +++ b/laravel/app/Listeners/SendUserShiftConfirmation.php @@ -46,10 +46,17 @@ public function handle(SlotChanged $event) $event_data = compact('slot', 'user_email', 'user_name', 'event_name', 'shift_name', 'start_date', 'start_time', 'end_time', 'admin_assigned'); - Mail::send('emails/user-shift-confirmation', $event_data, function ($message) use ($user_email, $user_name, $shift_name) + try { - $message->to($user_email, $user_name)->subject('Confirmation Email - ' . $shift_name . ' shift!'); - }); + Mail::send('emails/user-shift-confirmation', $event_data, function ($message) use ($user_email, $user_name, $shift_name) + { + $message->to($user_email, $user_name)->subject('Confirmation Email - ' . $shift_name . ' shift!'); + }); + } + catch (\Exception $exception) + { + app('request')->session()->flash('warning', "Unable to send email confirmation, SMTP error. Please notify the administrator of this volunteer database."); + } } } } diff --git a/laravel/app/Listeners/SendUserWelcome.php b/laravel/app/Listeners/SendUserWelcome.php index b5574f15..b4f5f56b 100644 --- a/laravel/app/Listeners/SendUserWelcome.php +++ b/laravel/app/Listeners/SendUserWelcome.php @@ -29,9 +29,16 @@ public function handle(UserRegistered $event) { $user = $event->user; - Mail::send('emails/user-welcome', compact('user'), function ($message) use ($user) + try { - $message->to($user->email, $user->name)->subject('Welcome to the Volunteer Database!'); - }); + Mail::send('emails/user-welcome', compact('user'), function ($message) use ($user) + { + $message->to($user->email, $user->name)->subject('Welcome to the Volunteer Database!'); + }); + } + catch (\Exception $exception) + { + app('request')->session()->flash('warning', "Unable to send email confirmation, SMTP error. Please notify the administrator of this volunteer database."); + } } } diff --git a/laravel/resources/views/app.blade.php b/laravel/resources/views/app.blade.php index 7ec33ce7..b0cb1eb9 100644 --- a/laravel/resources/views/app.blade.php +++ b/laravel/resources/views/app.blade.php @@ -39,6 +39,12 @@ @endif + @if(Session::has('warning')) + + @endif + @yield('content')