diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 9f2e82ff9..d101e0e8b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -57,6 +57,7 @@ jobs: MEILISEARCH_KEY: masterKey SCOUT_QUEUE: false #APP_DEBUG: true + #third party integration TEST_ZOHO_CLIENT_ID: ${{ secrets.TEST_ZOHO_CLIENT_ID }} TEST_ZOHO_CLIENT_SECRET: ${{ secrets.TEST_ZOHO_CLIENT_SECRET }} diff --git a/src/Kanvas/Apps/Support/SmtpRuntimeConfiguration.php b/src/Kanvas/Apps/Support/SmtpRuntimeConfiguration.php index 784050541..a828d3d84 100644 --- a/src/Kanvas/Apps/Support/SmtpRuntimeConfiguration.php +++ b/src/Kanvas/Apps/Support/SmtpRuntimeConfiguration.php @@ -7,27 +7,28 @@ use Baka\Contracts\AppInterface; use Baka\Contracts\CompanyInterface; use Baka\Contracts\HashTableInterface; +use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Config; class SmtpRuntimeConfiguration { protected string $appSmtp = 'appSmtp'; protected string $companySmtp = 'companySmtp'; - protected string $defaultSmtp; + protected array $defaultSmtp; public function __construct( protected AppInterface $app, protected ?CompanyInterface $company = null ) { - $this->defaultSmtp = config('mail.default'); + $this->defaultSmtp = config('mail.mailers.smtp'); } /** * Load SMTP settings from the given source. */ - protected function loadSmtpSettingsFromSource(string $provider, HashTableInterface $source): string + protected function loadSmtpSettingsFromSource(string $provider, HashTableInterface $source): array { - $config = [ + return [ 'transport' => 'smtp', 'host' => $source->get('smtp_host'), 'port' => $source->get('smtp_port'), @@ -37,16 +38,16 @@ protected function loadSmtpSettingsFromSource(string $provider, HashTableInterfa 'timeout' => null, ]; - Config::set('mail.mailers.' . $provider, $config); + //Config::set('mail.mailers.' . $provider, $config); - return $provider; + //return $provider; } /** * Load SMTP settings from the app. */ - protected function loadAppSettings(): string + protected function loadAppSettings(): array { return $this->loadSmtpSettingsFromSource($this->appSmtp, $this->app); } @@ -54,7 +55,7 @@ protected function loadAppSettings(): string /** * Load SMTP settings from the company config. */ - protected function loadCompanySettings(): string + protected function loadCompanySettings(): array { return $this->loadSmtpSettingsFromSource($this->companySmtp, $this->company); } @@ -63,7 +64,7 @@ protected function loadCompanySettings(): string * Determine the source of SMTP settings and load them. * Returns the SMTP settings source used. */ - public function loadSmtpSettings(): string + public function loadSmtpSettings(): array { if ($this->company !== null && $this->company->get('smtp_host')) { return $this->loadCompanySettings(); diff --git a/src/Kanvas/Notifications/KanvasMailable.php b/src/Kanvas/Notifications/KanvasMailable.php new file mode 100644 index 000000000..6cfdef076 --- /dev/null +++ b/src/Kanvas/Notifications/KanvasMailable.php @@ -0,0 +1,47 @@ + $this->emailContent, + ], + ); + } + + public function build(): self + { + if (app()->environment('testing')) { + // Skip setting the custom mailer configuration in testing environment + return $this; + } + + //thanks to https://github.com/laravel/framework/issues/42602#issuecomment-1143637921 + $customConfig = Mail::createSymfonyTransport($this->mailerConfig); + Mail::setSymfonyTransport($customConfig); + + return $this; + } +} diff --git a/src/Kanvas/Notifications/Notification.php b/src/Kanvas/Notifications/Notification.php index d436a70ad..a79803e43 100644 --- a/src/Kanvas/Notifications/Notification.php +++ b/src/Kanvas/Notifications/Notification.php @@ -11,8 +11,11 @@ use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Database\Eloquent\Model; +use Illuminate\Mail\Mailable; +use Illuminate\Notifications\AnonymousNotifiable; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Notification as LaravelNotification; +use Illuminate\Support\Facades\Config; use Kanvas\Apps\Models\Apps; use Kanvas\Apps\Support\SmtpRuntimeConfiguration; use Kanvas\Exceptions\ValidationException; @@ -142,20 +145,19 @@ public function via(object $notifiable): array * * @param mixed $notifiable */ - public function toMail($notifiable): ?MailMessage + public function toMail($notifiable): Mailable { $smtpConfiguration = new SmtpRuntimeConfiguration($this->app, $this->company); - $mailer = $smtpConfiguration->loadSmtpSettings(); + $mailConfig = $smtpConfiguration->loadSmtpSettings(); $fromMail = $smtpConfiguration->getFromEmail(); $fromEmail = $fromMail['address']; $fromName = $fromMail['name']; - $mailMessage = (new MailMessage()) - ->mailer($mailer) + $toEmail = $notifiable instanceof AnonymousNotifiable ? $notifiable->routes['mail'] : $notifiable->email; + $mailMessage = (new KanvasMailable($mailConfig, $this->getEmailContent())) ->from($fromEmail, $fromName) - //->subject($this->app->get('name') . ' - ' . $this->getTitle() - ->view('emails.layout', ['html' => $this->getEmailContent()]); + ->to($toEmail); $this->subject = $this->subject ?? $this->getNotificationTitle();