From 7d0432cb1bb7a6e989f6c4375f2e23240df1d347 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 16 Oct 2025 13:13:01 +0100 Subject: [PATCH] Added useLocale accessor for mail alerts --- app/Console/Commands/SendExpirationAlerts.php | 4 +-- .../Commands/SendUpcomingAuditReport.php | 2 +- app/Models/Setting.php | 31 +++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/app/Console/Commands/SendExpirationAlerts.php b/app/Console/Commands/SendExpirationAlerts.php index 7a97d39375a9..6a60f5bcfbba 100644 --- a/app/Console/Commands/SendExpirationAlerts.php +++ b/app/Console/Commands/SendExpirationAlerts.php @@ -57,7 +57,7 @@ public function handle() if ($assets->count() > 0) { - Mail::to($recipients)->send(new ExpiringAssetsMail($assets, $alert_interval)); + Mail::to($recipients)->locale($settings->use_locale)->send(new ExpiringAssetsMail($assets, $alert_interval)); $this->table( [ @@ -91,7 +91,7 @@ public function handle() ->orderBy('termination_date', 'ASC') ->get(); if ($licenses->count() > 0) { - Mail::to($recipients)->send(new ExpiringLicenseMail($licenses, $alert_interval)); + Mail::to($recipients)->locale($settings->use_locale)->send(new ExpiringLicenseMail($licenses, $alert_interval)); $this->table( [ diff --git a/app/Console/Commands/SendUpcomingAuditReport.php b/app/Console/Commands/SendUpcomingAuditReport.php index 4e422a1dcd20..e65cc263fd4a 100644 --- a/app/Console/Commands/SendUpcomingAuditReport.php +++ b/app/Console/Commands/SendUpcomingAuditReport.php @@ -60,7 +60,7 @@ public function handle() $this->info('Sending Admin SendUpcomingAuditNotification to: ' . $settings->alert_email); - Mail::to($recipients)->send(new SendUpcomingAuditMail($assets, $settings->audit_warning_days)); + Mail::to($recipients)->locale($settings->use_locale)->send(new SendUpcomingAuditMail($assets, $settings->audit_warning_days)); $this->table( [ diff --git a/app/Models/Setting.php b/app/Models/Setting.php index 4b96230027cd..ff35f04f081d 100755 --- a/app/Models/Setting.php +++ b/app/Models/Setting.php @@ -3,6 +3,7 @@ namespace App\Models; use Carbon\Carbon; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Notifications\Notifiable; @@ -120,6 +121,36 @@ public static function setupCompleted(): bool } } + /** + * Get the locale to use in some places in the app where we don't + * have access to the user context (i.e. email notifications where the alert address + * might not be an actual user object or it's being run via cli.) + * + * @return \Illuminate\Database\Eloquent\Casts\Attribute + */ + protected function useLocale(): Attribute + { + + return Attribute:: make( + get: function(mixed $value, array $attributes) { + + // Use current user's language + if ((request()->user()) && (request()->user()->locale)) { + return request()->user()->locale; + + // Fall back to app settings + } elseif ($attributes['locale'] != '') { + return $attributes['locale']; + } + + // Fall back to env + return config('app.locale'); + + } + ); + + } + /** * Get the current Laravel version. *