From db5272dd1d0a1c6d15fd156432d0311d29ce2c46 Mon Sep 17 00:00:00 2001 From: Ghassan Yusuf Date: Tue, 26 May 2026 12:26:02 +0300 Subject: [PATCH] feat: register dynamic mailers in AppServiceProvider, remove single-azure methods from SettingsController --- app/Http/Controllers/SettingsController.php | 68 +-------------------- app/Providers/AppServiceProvider.php | 23 ++++--- 2 files changed, 14 insertions(+), 77 deletions(-) diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php index f624acd..59d2511 100644 --- a/app/Http/Controllers/SettingsController.php +++ b/app/Http/Controllers/SettingsController.php @@ -5,9 +5,7 @@ namespace App\Http\Controllers; use App\Models\Setting; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Mail; use Illuminate\View\View; -use PromoSeven\AzureMailer\Graph\TokenManager; use PromoSeven\UltraMessage\Facades\UltraMessage; use PromoSeven\UltraMessage\UltraMessageException; @@ -23,15 +21,7 @@ class SettingsController extends Controller 'webhook_path' => Setting::get('ultramsg_webhook_path', 'ultra-message/webhook'), ]; - $azureSettings = [ - 'enabled' => Setting::get('azure_mail_enabled', false), - 'tenant_id' => Setting::get('azure_mail_tenant_id', ''), - 'client_id' => Setting::get('azure_mail_client_id', ''), - 'client_secret' => Setting::get('azure_mail_client_secret', ''), - 'from_address' => Setting::get('azure_mail_from_address', ''), - ]; - - return view('settings.integrations', compact('whatsappSettings', 'azureSettings')); + return view('settings.integrations', compact('whatsappSettings')); } public function updateWhatsapp(Request $request): JsonResponse @@ -52,62 +42,6 @@ class SettingsController extends Controller return response()->json(['success' => true]); } - public function updateAzureMail(Request $request): JsonResponse - { - $request->validate([ - 'tenant_id' => ['required', 'string', 'max:100'], - 'client_id' => ['required', 'string', 'max:100'], - 'client_secret' => ['required', 'string', 'max:500'], - 'from_address' => ['required', 'email', 'max:255'], - ]); - - Setting::set('azure_mail_enabled', $request->input('enabled') === '1' ? '1' : '0'); - Setting::set('azure_mail_tenant_id', $request->tenant_id); - Setting::set('azure_mail_client_id', $request->client_id); - Setting::set('azure_mail_client_secret', $request->client_secret); - Setting::set('azure_mail_from_address', $request->from_address); - - return response()->json(['success' => true]); - } - - public function testAzureMailConnection(): JsonResponse - { - try { - $config = [ - 'tenant_id' => Setting::get('azure_mail_tenant_id', ''), - 'client_id' => Setting::get('azure_mail_client_id', ''), - 'client_secret' => Setting::get('azure_mail_client_secret', ''), - ]; - $tokenManager = new TokenManager($config); - $tokenManager->getToken(); - return response()->json(['success' => true]); - } catch (\Exception $e) { - return response()->json(['success' => false, 'message' => $e->getMessage()]); - } - } - - public function sendTestEmail(Request $request): JsonResponse - { - $request->validate([ - 'to' => ['required', 'email', 'max:255'], - 'subject' => ['required', 'string', 'max:255'], - ]); - - try { - $to = $request->to; - $subject = $request->subject; - Mail::mailer('azure')->raw( - 'This is a test email from SteelERP.', - function ($message) use ($to, $subject) { - $message->to($to)->subject($subject); - } - ); - return response()->json(['success' => true]); - } catch (\Exception $e) { - return response()->json(['success' => false, 'message' => $e->getMessage()]); - } - } - public function testWhatsappConnection(): JsonResponse { try { diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 290db1c..6e9a61a 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,23 +2,16 @@ namespace App\Providers; +use App\Models\MailAccount; use App\Models\Setting; +use Illuminate\Mail\MailManager; use Illuminate\Support\ServiceProvider; use PromoSeven\UltraMessage\Facades\UltraMessage; class AppServiceProvider extends ServiceProvider { - /** - * Register any application services. - */ - public function register(): void - { - // - } + public function register(): void {} - /** - * Bootstrap any application services. - */ public function boot(): void { UltraMessage::configUsing(function () { @@ -31,5 +24,15 @@ class AppServiceProvider extends ServiceProvider 'enabled' => (bool) Setting::get('ultramsg_enabled', config('ultra-message.enabled', true)), ]; }); + + $this->callAfterResolving(MailManager::class, function (MailManager $manager) { + try { + foreach (MailAccount::all() as $account) { + $manager->extend($account->name, fn () => $account->buildTransport()); + } + } catch (\Exception) { + // DB not ready on fresh install — skip silently + } + }); } }