A free open source IT asset/license management system
Better handle bad email settings
snipef58be8c27a5a
diff --git a/app/Listeners/CheckoutableListener.php b/app/Listeners/CheckoutableListener.phpindex 2fe9b9f964..d346cc7e99 100644--- a/app/Listeners/CheckoutableListener.php+++ b/app/Listeners/CheckoutableListener.php@@ -34,7 +34,6 @@ use App\Notifications\CheckoutAssetNotification;use App\Notifications\CheckoutComponentNotification;use App\Notifications\CheckoutConsumableNotification;use App\Notifications\CheckoutLicenseSeatNotification;-use Exception;use GuzzleHttp\Exception\ClientException;use Illuminate\Database\Eloquent\Collection;use Illuminate\Database\Eloquent\Model;@@ -110,9 +109,8 @@ class CheckoutableListener$toMail = (clone $mailable)->locale($notifiable?->locale);Mail::to(array_flatten($to))->send($toMail);Log::info('Checkout Mail sent to checkout target');- } catch (ClientException $e) {- Log::debug('Exception caught during checkout email: '.$e->getMessage());- } catch (Exception $e) {+ } catch (\Throwable $e) {+ // Throwable catches both Exception and Error subtypes.Log::debug('Exception caught during checkout email: '.$e->getMessage());}}@@ -120,9 +118,7 @@ class CheckoutableListenertry {$ccMail = (clone $mailable)->locale(Setting::getSettings()->locale);Mail::cc(array_flatten($cc))->send($ccMail);- } catch (ClientException $e) {- Log::debug('Exception caught during checkout email: '.$e->getMessage());- } catch (Exception $e) {+ } catch (\Throwable $e) {Log::debug('Exception caught during checkout email: '.$e->getMessage());}}@@ -158,7 +154,7 @@ class CheckoutableListener}return redirect()->back()->with('warning', ucfirst($webhookSource->webhook_selected).trans('admin/settings/message.webhook.webhook_fail'));- } catch (Exception $e) {+ } catch (\Throwable $e) {Log::warning(ucfirst($webhookSource->webhook_selected).' webhook notification failed:', ['error' => $e->getMessage(),'webhook_endpoint' => $webhookSource->webhook_endpoint,@@ -210,9 +206,10 @@ class CheckoutableListener$toMail = (clone $mailable)->locale($notifiable?->locale);Mail::to(array_flatten($to))->send($toMail);Log::info('Checkin Mail sent to checkin target');- } catch (ClientException $e) {- Log::debug('Exception caught during checkin email: '.$e->getMessage());- } catch (Exception $e) {+ } catch (\Throwable $e) {+ // See the matching catch in onCheckedOut. Mail is+ // best-effort and transport-level failures must not+ // 500 the checkin / transfer flow.Log::debug('Exception caught during checkin email: '.$e->getMessage());}}@@ -220,9 +217,7 @@ class CheckoutableListenertry {$ccMail = (clone $mailable)->locale(Setting::getSettings()->locale);Mail::cc(array_flatten($cc))->send($ccMail);- } catch (ClientException $e) {- Log::debug('Exception caught during checkin email: '.$e->getMessage());- } catch (Exception $e) {+ } catch (\Throwable $e) {Log::debug('Exception caught during checkin email: '.$e->getMessage());}}@@ -256,7 +251,7 @@ class CheckoutableListenerreturn redirect()->back()->with('warning', ucfirst($webhookSource->webhook_selected).trans('admin/settings/message.webhook.webhook_fail'));}}- } catch (Exception $e) {+ } catch (\Throwable $e) {Log::warning(ucfirst($webhookSource->webhook_selected).' webhook notification failed:', ['error' => $e->getMessage(),'webhook_endpoint' => $webhookSource->webhook_endpoint,diff --git a/config/mail.php b/config/mail.phpindex 6878cc3e31..8c6498d211 100755--- a/config/mail.php+++ b/config/mail.php@@ -92,8 +92,8 @@ return [| stay compatible with the Mailgun e-mail application by default.|*/-- 'port' => env('MAIL_PORT', 587),++ 'port' => (int) env('MAIL_PORT', 587),/*|--------------------------------------------------------------------------@@ -229,10 +229,10 @@ return [|| Uses modern app authentication for Microsoft 365 tenants instead of the| less secure SMTP authenticaion.- |+ || Instructions can be found here:| https://learn.microsoft.com/en-us/graph/auth-register-app-v2- |+ || It's also recommended that you configure RBAC to limit this application's| scope to approved mailboxes.| https://learn.microsoft.com/en-us/exchange/permissions-exo/application-rbac@@ -256,7 +256,7 @@ return [|--------------------------------------------------------------------------|| You will need to create a client secret for the Entra App Registration.- | The instructions are found at the URL above under 'Add credentials'+ | The instructions are found at the URL above under 'Add credentials'| option 2.|*/@@ -275,7 +275,7 @@ return [| Save to Sent Items|--------------------------------------------------------------------------|- | This controls whether or not mail sent using this driver is saved in the+ | This controls whether or not mail sent using this driver is saved in the| mailbox's Sent folder. Defaults to false.|*/diff --git a/tests/Feature/Checkins/Api/AssetCheckinMailTransportErrorTest.php b/tests/Feature/Checkins/Api/AssetCheckinMailTransportErrorTest.phpnew file mode 100644index 0000000000..cc93438b3a--- /dev/null+++ b/tests/Feature/Checkins/Api/AssetCheckinMailTransportErrorTest.php@@ -0,0 +1,51 @@+<?php++namespace Tests\Feature\Checkins\Api;++use App\Models\Asset;+use App\Models\Category;+use App\Models\User;+use Illuminate\Support\Facades\Mail;+use Tests\TestCase;++/**+ * Regression for issue #20009.+ *+ * A broken mail transport (unresolved Docker-link `MAIL_PORT`, bad host,+ * stray TypeError from a strict-typed library) must not take down the+ * parent checkin / transfer request. CheckoutableListener wraps its+ * Mail::send calls in try/catch, but previously only caught Exception,+ * which left Error subtypes (TypeError in particular) bubbling up as a+ * 500. The listener is now Throwable-catching so transport-level+ * failures log and move on while the asset is still checked in.+ */+class AssetCheckinMailTransportErrorTest extends TestCase+{+ public function test_broken_mail_transport_does_not_500_the_checkin(): void+ {+ // Category that triggers the mail path on checkin so the+ // listener actually reaches Mail::send during the request.+ // Without a mail-triggering category, the listener early-exits+ // and the TypeError never has a chance to fire.+ $category = Category::factory()->forAssets()->sendsCheckinEmail()->create();+ $assignee = User::factory()->create(['email' => '[email protected]']);+ $asset = Asset::factory()+ ->for(\App\Models\AssetModel::factory()->for($category, 'category'), 'model')+ ->assignedToUser($assignee)+ ->create();++ // Simulate the transport-level TypeError that unresolved Docker+ // link vars like MAIL_PORT=${MAIL_PORT_587_TCP_PORT} produce.+ // Any Throwable raised inside the listener's Mail::send block+ // should be swallowed by the try/catch.+ Mail::shouldReceive('to')->andThrow(new \TypeError('simulated Dsn port TypeError'));++ $this->actingAsForApi(User::factory()->checkinAssets()->create())+ ->postJson(route('api.asset.checkin', $asset))+ ->assertOk()+ ->assertStatusMessageIs('success');++ $this->assertNull($asset->refresh()->assignedTo, 'Asset should be checked in despite the mail transport error.');+ $this->assertNull($asset->assigned_type);+ }+}