channel) { /** ===================== SMS ===================== */ 'sms' => $sms->send( phone: $this->data['phone'], template: $this->template, data: $this->data ), /** ===================== WHATSAPP ===================== */ 'whatsapp' => $whatsapp->send( phone: $this->data['phone'], template: $this->template, businessId: $this->businessId, name: $this->data['name'] ?? '', data: $this->data ), /** ===================== EMAIL ===================== */ 'email' => $this->sendEmail(), default => throw new \Exception('Invalid notification channel'), }; MessageReport::where('id', $this->notificationId)->update([ 'status' => 'success', 'send_at' => now(), ]); } catch (Throwable $e) { MessageReport::where('id', $this->notificationId)->update([ 'status' => 'failed', 'send_at' => now(), ]); throw $e; } } /** * Handle email sending with dynamic SMTP */ protected function sendEmail(): void { $smtp = EmailSetting::where('business_id', $this->businessId)->first(); if ($smtp) { Config::set('mail.mailers.smtp.transport', $smtp->driver); Config::set('mail.mailers.smtp.host', $smtp->host); Config::set('mail.mailers.smtp.port', $smtp->port); Config::set('mail.mailers.smtp.username', $smtp->username); Config::set('mail.mailers.smtp.password', $smtp->password); Config::set('mail.mailers.smtp.encryption', $smtp->encryption); Config::set('mail.from.address', $smtp->from_address); Config::set('mail.from.name', $smtp->from_name); // IMPORTANT: reset mailer to avoid queue worker cache issues Mail::forgetMailers(); Mail::to($this->data['email']) ->send(new DynamicMail($this->template, $this->data)); } else { MessageReport::where('id', $this->notificationId)->update([ 'status' => 'failed', 'send_at' => now(), ]); } } /** * Called automatically by Laravel after final failure */ public function failed(): void { MessageReport::where('id', $this->notificationId)->update([ 'status' => 'failed', 'send_at' => now(), ]); } }