Skip to content

Commit

Permalink
Merge pull request #1682 from bakaphp/hotfix-smtp-configuration
Browse files Browse the repository at this point in the history
fix: smtp configuration per app
  • Loading branch information
kaioken committed Jul 15, 2024
2 parents d08fbfb + 67a7a98 commit d354705
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 15 deletions.
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
19 changes: 10 additions & 9 deletions src/Kanvas/Apps/Support/SmtpRuntimeConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -37,24 +38,24 @@ 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);
}

/**
* Load SMTP settings from the company config.
*/
protected function loadCompanySettings(): string
protected function loadCompanySettings(): array
{
return $this->loadSmtpSettingsFromSource($this->companySmtp, $this->company);
}
Expand All @@ -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();
Expand Down
47 changes: 47 additions & 0 deletions src/Kanvas/Notifications/KanvasMailable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Kanvas\Notifications;

use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Support\Facades\Mail;

/**
* we create a custom Mailable class to set the mailer configuration
* at runtime, this is useful when we need to send emails using different
* mailer configurations.
*/
class KanvasMailable extends Mailable
{
public function __construct(
protected array $mailerConfig,
protected string $emailContent
) {
}

public function content(): Content
{
return new Content(
view: 'emails.layout',
with: [
'html' => $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;
}
}
14 changes: 8 additions & 6 deletions src/Kanvas/Notifications/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit d354705

Please sign in to comment.