A free open source IT asset/license management system
Wrap sends in try/catch, log if failed
snipe3cdeeefef311
diff --git a/app/Console/Commands/SendAcceptanceReminder.php b/app/Console/Commands/SendAcceptanceReminder.phpindex 5e4ea97057..801b48193d 100644--- a/app/Console/Commands/SendAcceptanceReminder.php+++ b/app/Console/Commands/SendAcceptanceReminder.php@@ -12,7 +12,9 @@ use App\Models\LicenseSeat;use App\Models\User;use Illuminate\Console\Command;use Illuminate\Database\Eloquent\Relations\MorphTo;+use Illuminate\Support\Facades\Log;use Illuminate\Support\Facades\Mail;+use Symfony\Component\Mailer\Exception\TransportException;class SendAcceptanceReminder extends Command{@@ -70,6 +72,7 @@ class SendAcceptanceReminder extends Command->get();$count = 0;+ $failed = 0;$unacceptedAssetGroups = $pending->map(function ($acceptance) {return ['assetItem' => $acceptance->checkoutable, 'acceptance' => $acceptance];@@ -92,20 +95,31 @@ class SendAcceptanceReminder extends Command'id' => $acceptance->assignedTo?->id,'name' => $acceptance->assignedTo?->display_name,];- } else {- $count++;++ continue;}+$item_count = $unacceptedAssetGroup->count();- if ($locale && $email) {- Mail::to($email)->send((new UnacceptedAssetReminderMail($acceptance, $item_count))->locale($locale));- } elseif ($email) {- Mail::to($email)->send((new UnacceptedAssetReminderMail($acceptance, $item_count)));+ try {+ if ($locale) {+ Mail::to($email)->send((new UnacceptedAssetReminderMail($acceptance, $item_count))->locale($locale));+ } else {+ Mail::to($email)->send((new UnacceptedAssetReminderMail($acceptance, $item_count)));+ }+ $count++;+ } catch (TransportException $e) {+ $failed++;+ $message = 'Failed to send acceptance reminder to '.$email.': '.$e->getMessage();+ Log::warning($message);+ $this->error($message);}-}$this->info($count.' users notified.');+ if ($failed > 0) {+ $this->warn($failed.' reminder(s) failed to send due to mail transport errors. See log for details.');+ }$headers = ['ID', 'Name'];$rows = [];diff --git a/app/Console/Commands/SendCurrentInventoryToUsers.php b/app/Console/Commands/SendCurrentInventoryToUsers.phpindex c5180203e6..3c0f04c08a 100644--- a/app/Console/Commands/SendCurrentInventoryToUsers.php+++ b/app/Console/Commands/SendCurrentInventoryToUsers.php@@ -5,6 +5,8 @@ namespace App\Console\Commands;use App\Models\User;use App\Notifications\CurrentInventory;use Illuminate\Console\Command;+use Illuminate\Support\Facades\Log;+use Symfony\Component\Mailer\Exception\TransportException;class SendCurrentInventoryToUsers extends Command{@@ -42,13 +44,24 @@ class SendCurrentInventoryToUsers extends Command$users = User::whereNull('deleted_at')->whereNotNull('email')->with('assets', 'accessories', 'licenses')->get();$count = 0;+ $failed = 0;foreach ($users as $user) {if (($user->assets->count() > 0) || ($user->accessories->count() > 0) || ($user->licenses->count() > 0) || ($user->consumables->count() > 0)) {- $count++;- $user->notify((new CurrentInventory($user)));+ try {+ $user->notify((new CurrentInventory($user)));+ $count++;+ } catch (TransportException $e) {+ $failed++;+ $message = 'Failed to send current-inventory notification to '.$user->email.': '.$e->getMessage();+ Log::warning($message);+ $this->error($message);+ }}}$this->info($count.' users notified.');+ if ($failed > 0) {+ $this->warn($failed.' notification(s) failed to send due to mail transport errors. See log for details.');+ }}}diff --git a/app/Console/Commands/SendExpectedCheckinAlerts.php b/app/Console/Commands/SendExpectedCheckinAlerts.phpindex bcc980e1cf..539e23a0c2 100644--- a/app/Console/Commands/SendExpectedCheckinAlerts.php+++ b/app/Console/Commands/SendExpectedCheckinAlerts.php@@ -10,7 +10,9 @@ use App\Notifications\ExpectedCheckinAdminNotification;use App\Notifications\ExpectedCheckinNotification;use Carbon\Carbon;use Illuminate\Console\Command;+use Illuminate\Support\Facades\Log;use Illuminate\Support\Facades\Notification;+use Symfony\Component\Mailer\Exception\TransportException;class SendExpectedCheckinAlerts extends Command{@@ -57,10 +59,20 @@ class SendExpectedCheckinAlerts extends Command$this->info($assets->count().' assets must be checked on or before '.Helper::getFormattedDateObject($interval_date, 'date', false));+ $failed = 0;foreach ($assets as $asset) {if ($asset->assignedTo && (isset($asset->assignedTo->email)) && ($asset->assignedTo->email != '') && $asset->checkedOutToUser()) {- $asset->assignedTo->notify((new ExpectedCheckinNotification($asset)));- $count++;+ try {+ $asset->assignedTo->notify((new ExpectedCheckinNotification($asset)));+ $count++;+ } catch (TransportException $e) {+ // Mail transport failed (bad SMTP creds, DNS, connection refused).+ // Log and keep going so one broken send doesn't strand every remaining user.+ $failed++;+ $message = 'Failed to send expected-checkin notification to '.$asset->assignedTo->email.' for asset '.$asset->id.': '.$e->getMessage();+ Log::warning($message);+ $this->error($message);+ }}}@@ -92,11 +104,19 @@ class SendExpectedCheckinAlerts extends Command$recipients = collect(explode(',', $settings->alert_email))->map(function ($item) {return new AlertRecipient($item);});- Notification::send($recipients, new ExpectedCheckinAdminNotification($assets));-+ try {+ Notification::send($recipients, new ExpectedCheckinAdminNotification($assets));+ } catch (TransportException $e) {+ $message = 'Failed to send expected-checkin admin rollup: '.$e->getMessage();+ Log::warning($message);+ $this->error($message);+ }}- $this->info('Sent checkin reminders to to '.$count.' users.');+ $this->info('Sent checkin reminders to '.$count.' users.');+ if ($failed > 0) {+ $this->warn($failed.' notification(s) failed to send due to mail transport errors. See log for details.');+ }}}diff --git a/app/Console/Commands/SendExpirationAlerts.php b/app/Console/Commands/SendExpirationAlerts.phpindex d2905fc426..db84784977 100644--- a/app/Console/Commands/SendExpirationAlerts.php+++ b/app/Console/Commands/SendExpirationAlerts.php@@ -8,7 +8,9 @@ use App\Models\Asset;use App\Models\License;use App\Models\Setting;use Illuminate\Console\Command;+use Illuminate\Support\Facades\Log;use Illuminate\Support\Facades\Mail;+use Symfony\Component\Mailer\Exception\TransportException;class SendExpirationAlerts extends Command{@@ -58,7 +60,13 @@ class SendExpirationAlerts extends Commandif ($assets->count() > 0) {- Mail::to($recipients)->send(new ExpiringAssetsMail($assets, $alert_interval));+ try {+ Mail::to($recipients)->send(new ExpiringAssetsMail($assets, $alert_interval));+ } catch (TransportException $e) {+ $message = 'Failed to send expiring-assets alert: '.$e->getMessage();+ Log::warning($message);+ $this->error($message);+ }$this->table([@@ -91,7 +99,13 @@ class SendExpirationAlerts extends Command->orderBy('termination_date', 'ASC')->get();if ($licenses->count() > 0) {- Mail::to($recipients)->send(new ExpiringLicenseMail($licenses, $alert_interval));+ try {+ Mail::to($recipients)->send(new ExpiringLicenseMail($licenses, $alert_interval));+ } catch (TransportException $e) {+ $message = 'Failed to send expiring-licenses alert: '.$e->getMessage();+ Log::warning($message);+ $this->error($message);+ }$this->table([diff --git a/app/Console/Commands/SendInventoryAlerts.php b/app/Console/Commands/SendInventoryAlerts.phpindex 40d92ec153..72b9506a23 100644--- a/app/Console/Commands/SendInventoryAlerts.php+++ b/app/Console/Commands/SendInventoryAlerts.php@@ -7,7 +7,9 @@ use App\Models\Recipients\AlertRecipient;use App\Models\Setting;use App\Notifications\InventoryAlert;use Illuminate\Console\Command;+use Illuminate\Support\Facades\Log;use Illuminate\Support\Facades\Notification;+use Symfony\Component\Mailer\Exception\TransportException;class SendInventoryAlerts extends Command{@@ -52,7 +54,13 @@ class SendInventoryAlerts extends Commandreturn new AlertRecipient($item);});- Notification::send($recipients, new InventoryAlert($items, $settings->alert_threshold));+ try {+ Notification::send($recipients, new InventoryAlert($items, $settings->alert_threshold));+ } catch (TransportException $e) {+ $message = 'Failed to send inventory alert: '.$e->getMessage();+ Log::warning($message);+ $this->error($message);+ }} else {$this->info('No low inventory items found. No mail sent.');}diff --git a/app/Console/Commands/SendUpcomingAuditReport.php b/app/Console/Commands/SendUpcomingAuditReport.phpindex 21b5c5b8d0..9f5598fcba 100644--- a/app/Console/Commands/SendUpcomingAuditReport.php+++ b/app/Console/Commands/SendUpcomingAuditReport.php@@ -7,7 +7,9 @@ use App\Models\Asset;use App\Models\Setting;use Carbon\Carbon;use Illuminate\Console\Command;+use Illuminate\Support\Facades\Log;use Illuminate\Support\Facades\Mail;+use Symfony\Component\Mailer\Exception\TransportException;class SendUpcomingAuditReport extends Command{@@ -66,8 +68,14 @@ class SendUpcomingAuditReport extends Command->filter(fn ($item) => ! empty($item))->all();- Mail::to($recipients)->send(new SendUpcomingAuditMail($assets_for_email, $settings->audit_warning_days, $asset_count));- $this->info('Audit notification sent to: '.$settings->alert_email);+ try {+ Mail::to($recipients)->send(new SendUpcomingAuditMail($assets_for_email, $settings->audit_warning_days, $asset_count));+ $this->info('Audit notification sent to: '.$settings->alert_email);+ } catch (TransportException $e) {+ $message = 'Failed to send upcoming-audit alert: '.$e->getMessage();+ Log::warning($message);+ $this->error($message);+ }} else {$this->info('There is no admin alert email set so no email will be sent.');