From 2bfe67c028685a056e778f906356cf4f5b7872ac Mon Sep 17 00:00:00 2001 From: kaioken Date: Sun, 14 Jul 2024 20:58:20 -0400 Subject: [PATCH 1/9] fix: smtp configuration per app --- .../Apps/Support/SmtpRuntimeConfiguration.php | 19 +++++---- src/Kanvas/Notifications/KanvasMailable.php | 42 +++++++++++++++++++ src/Kanvas/Notifications/Notification.php | 14 ++++--- 3 files changed, 60 insertions(+), 15 deletions(-) create mode 100644 src/Kanvas/Notifications/KanvasMailable.php 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..7c2e8d799 --- /dev/null +++ b/src/Kanvas/Notifications/KanvasMailable.php @@ -0,0 +1,42 @@ + $this->emailContent, + ], + ); + } + + public function build(): self + { + //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(); From 05f704816ee70836389929f182c74c68368a4a23 Mon Sep 17 00:00:00 2001 From: kaioken Date: Sun, 14 Jul 2024 21:22:41 -0400 Subject: [PATCH 2/9] fix: smtp configuration per app --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 9f2e82ff9..24a05aef8 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -160,7 +160,7 @@ jobs: - name: Run Tests if: success() - run: php artisan test + run: php artisan test --debug - name: Upload artifacts uses: actions/upload-artifact@master From 6cc20de66b7169e105f8cc8efda349a5de0c2fa8 Mon Sep 17 00:00:00 2001 From: kaioken Date: Sun, 14 Jul 2024 21:29:52 -0400 Subject: [PATCH 3/9] fix: smtp configuration per app --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 24a05aef8..600fc674a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -160,7 +160,7 @@ jobs: - name: Run Tests if: success() - run: php artisan test --debug + run: php artisan test --verbose - name: Upload artifacts uses: actions/upload-artifact@master From efee3c03daf88c4bb4a35bcbfaedb01970cce97e Mon Sep 17 00:00:00 2001 From: kaioken Date: Sun, 14 Jul 2024 21:37:40 -0400 Subject: [PATCH 4/9] fix: smtp configuration per app --- .github/workflows/tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 600fc674a..990b16379 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -56,7 +56,7 @@ jobs: MEILISEARCH_HOST: http://localhost:7700 MEILISEARCH_KEY: masterKey SCOUT_QUEUE: false - #APP_DEBUG: true + APP_DEBUG: true #third party integration TEST_ZOHO_CLIENT_ID: ${{ secrets.TEST_ZOHO_CLIENT_ID }} TEST_ZOHO_CLIENT_SECRET: ${{ secrets.TEST_ZOHO_CLIENT_SECRET }} @@ -160,7 +160,7 @@ jobs: - name: Run Tests if: success() - run: php artisan test --verbose + run: php artisan test - name: Upload artifacts uses: actions/upload-artifact@master From 96018b2ebecb65ec47d101005e3e0d84c17d71e1 Mon Sep 17 00:00:00 2001 From: kaioken Date: Sun, 14 Jul 2024 21:45:32 -0400 Subject: [PATCH 5/9] fix: smtp configuration per app --- .github/workflows/tests.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 990b16379..c0e86bdd0 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -57,6 +57,8 @@ jobs: MEILISEARCH_KEY: masterKey SCOUT_QUEUE: false APP_DEBUG: true + MAIL_MAILER: null + #third party integration TEST_ZOHO_CLIENT_ID: ${{ secrets.TEST_ZOHO_CLIENT_ID }} TEST_ZOHO_CLIENT_SECRET: ${{ secrets.TEST_ZOHO_CLIENT_SECRET }} From 67241bfcd5d3a480ea90a5c3e4652a386f87c4fd Mon Sep 17 00:00:00 2001 From: kaioken Date: Sun, 14 Jul 2024 21:53:02 -0400 Subject: [PATCH 6/9] fix: smtp configuration per app --- .github/workflows/tests.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index c0e86bdd0..3bcc7c546 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -57,8 +57,7 @@ jobs: MEILISEARCH_KEY: masterKey SCOUT_QUEUE: false APP_DEBUG: true - MAIL_MAILER: null - + #third party integration TEST_ZOHO_CLIENT_ID: ${{ secrets.TEST_ZOHO_CLIENT_ID }} TEST_ZOHO_CLIENT_SECRET: ${{ secrets.TEST_ZOHO_CLIENT_SECRET }} From d13c02d55123ad63c00fb7210929d1dfb7f6c69c Mon Sep 17 00:00:00 2001 From: kaioken Date: Sun, 14 Jul 2024 22:03:33 -0400 Subject: [PATCH 7/9] fix: smtp configuration per app --- src/Kanvas/Notifications/KanvasMailable.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Kanvas/Notifications/KanvasMailable.php b/src/Kanvas/Notifications/KanvasMailable.php index 7c2e8d799..6cfdef076 100644 --- a/src/Kanvas/Notifications/KanvasMailable.php +++ b/src/Kanvas/Notifications/KanvasMailable.php @@ -33,6 +33,11 @@ public function content(): Content 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); From 1465bac859a3f8ce6672357c43e7b75d2eab4337 Mon Sep 17 00:00:00 2001 From: kaioken Date: Sun, 14 Jul 2024 22:09:59 -0400 Subject: [PATCH 8/9] fix: smtp configuration per app --- .github/workflows/tests.yml | 2 +- src/Kanvas/Notifications/KanvasMailable.php | 2 +- tests/TestCase.php | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 3bcc7c546..d101e0e8b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -56,7 +56,7 @@ jobs: MEILISEARCH_HOST: http://localhost:7700 MEILISEARCH_KEY: masterKey SCOUT_QUEUE: false - APP_DEBUG: true + #APP_DEBUG: true #third party integration TEST_ZOHO_CLIENT_ID: ${{ secrets.TEST_ZOHO_CLIENT_ID }} diff --git a/src/Kanvas/Notifications/KanvasMailable.php b/src/Kanvas/Notifications/KanvasMailable.php index 6cfdef076..79f3b1209 100644 --- a/src/Kanvas/Notifications/KanvasMailable.php +++ b/src/Kanvas/Notifications/KanvasMailable.php @@ -35,7 +35,7 @@ public function build(): self { if (app()->environment('testing')) { // Skip setting the custom mailer configuration in testing environment - return $this; + //return $this; } //thanks to https://github.com/laravel/framework/issues/42602#issuecomment-1143637921 diff --git a/tests/TestCase.php b/tests/TestCase.php index ebaaad3b2..05673bd0f 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -3,16 +3,31 @@ namespace Tests; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; +use Illuminate\Support\Facades\Mail; use Kanvas\Auth\Actions\RegisterUsersAction; use Kanvas\Auth\DataTransferObject\RegisterInput as RegisterPostDataDto; use Kanvas\Users\Models\Users; use Nuwave\Lighthouse\Testing\MakesGraphQLRequests; +use Symfony\Component\Mailer\Transport\TransportInterface; class TestCase extends BaseTestCase { use CreatesApplication; use MakesGraphQLRequests; + protected function setUp(): void + { + parent::setUp(); + + // Mock the createSymfonyTransport method + Mail::shouldReceive('createSymfonyTransport') + ->andReturn(\Mockery::mock(TransportInterface::class)); + + // Mock the setSymfonyTransport method + Mail::shouldReceive('setSymfonyTransport') + ->andReturnNull(); + } + /** * createUser. * From 67a7a981de3bd00ba112569b0aff753eb793f526 Mon Sep 17 00:00:00 2001 From: kaioken Date: Sun, 14 Jul 2024 22:15:56 -0400 Subject: [PATCH 9/9] fix: smtp configuration per app --- src/Kanvas/Notifications/KanvasMailable.php | 2 +- tests/TestCase.php | 15 --------------- 2 files changed, 1 insertion(+), 16 deletions(-) diff --git a/src/Kanvas/Notifications/KanvasMailable.php b/src/Kanvas/Notifications/KanvasMailable.php index 79f3b1209..6cfdef076 100644 --- a/src/Kanvas/Notifications/KanvasMailable.php +++ b/src/Kanvas/Notifications/KanvasMailable.php @@ -35,7 +35,7 @@ public function build(): self { if (app()->environment('testing')) { // Skip setting the custom mailer configuration in testing environment - //return $this; + return $this; } //thanks to https://github.com/laravel/framework/issues/42602#issuecomment-1143637921 diff --git a/tests/TestCase.php b/tests/TestCase.php index 05673bd0f..ebaaad3b2 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -3,31 +3,16 @@ namespace Tests; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; -use Illuminate\Support\Facades\Mail; use Kanvas\Auth\Actions\RegisterUsersAction; use Kanvas\Auth\DataTransferObject\RegisterInput as RegisterPostDataDto; use Kanvas\Users\Models\Users; use Nuwave\Lighthouse\Testing\MakesGraphQLRequests; -use Symfony\Component\Mailer\Transport\TransportInterface; class TestCase extends BaseTestCase { use CreatesApplication; use MakesGraphQLRequests; - protected function setUp(): void - { - parent::setUp(); - - // Mock the createSymfonyTransport method - Mail::shouldReceive('createSymfonyTransport') - ->andReturn(\Mockery::mock(TransportInterface::class)); - - // Mock the setSymfonyTransport method - Mail::shouldReceive('setSymfonyTransport') - ->andReturnNull(); - } - /** * createUser. *