diff --git a/api/app/Integrations/Handlers/AbstractEmailIntegrationHandler.php b/api/app/Integrations/Handlers/AbstractEmailIntegrationHandler.php deleted file mode 100644 index d9c7ea68..00000000 --- a/api/app/Integrations/Handlers/AbstractEmailIntegrationHandler.php +++ /dev/null @@ -1,47 +0,0 @@ -initializeMailer(); - } - - protected function initializeMailer() - { - $this->mailer = config('mail.default'); - $this->setWorkspaceSMTPSettings(); - - if (!$this->mailer) { - Log::error('Mailer not specified', [ - 'form_id' => $this->form->id - ]); - } - } - - protected function setWorkspaceSMTPSettings() - { - $workspace = $this->form->workspace; - $emailSettings = $workspace->settings['email_settings'] ?? []; - if (!$workspace->is_pro || !$emailSettings || empty($emailSettings['host']) || empty($emailSettings['port']) || empty($emailSettings['username']) || empty($emailSettings['password'])) { - return; - } - - config([ - 'mail.mailers.custom_smtp.host' => $emailSettings['host'], - 'mail.mailers.custom_smtp.port' => $emailSettings['port'], - 'mail.mailers.custom_smtp.username' => $emailSettings['username'], - 'mail.mailers.custom_smtp.password' => $emailSettings['password'] - ]); - $this->mailer = 'custom_smtp'; - } -} diff --git a/api/app/Integrations/Handlers/EmailIntegration.php b/api/app/Integrations/Handlers/EmailIntegration.php index 834f63db..f6cfd34e 100644 --- a/api/app/Integrations/Handlers/EmailIntegration.php +++ b/api/app/Integrations/Handlers/EmailIntegration.php @@ -11,7 +11,7 @@ use App\Open\MentionParser; use App\Service\Forms\FormSubmissionFormatter; use Illuminate\Validation\ValidationException; -class EmailIntegration extends AbstractEmailIntegrationHandler +class EmailIntegration extends AbstractIntegrationHandler { public const RISKY_USERS_LIMIT = 120; @@ -95,16 +95,15 @@ class EmailIntegration extends AbstractEmailIntegrationHandler ->filter(function ($email) { return filter_var($email, FILTER_VALIDATE_EMAIL); }); - Log::debug('Sending email notification', [ + Log::info('Sending email notification', [ 'recipients' => $recipients->toArray(), 'form_id' => $this->form->id, 'form_slug' => $this->form->slug, - 'mailer' => $this->mailer ]); $recipients->each(function ($subscriber) { Notification::route('mail', $subscriber)->notify( - new FormEmailNotification($this->event, $this->integrationData, $this->mailer) + new FormEmailNotification($this->event, $this->integrationData) ); }); } diff --git a/api/app/Notifications/Forms/FormEmailNotification.php b/api/app/Notifications/Forms/FormEmailNotification.php index a616c93f..5f053c2f 100644 --- a/api/app/Notifications/Forms/FormEmailNotification.php +++ b/api/app/Notifications/Forms/FormEmailNotification.php @@ -5,30 +5,49 @@ namespace App\Notifications\Forms; use App\Events\Forms\FormSubmitted; use App\Open\MentionParser; use App\Service\Forms\FormSubmissionFormatter; -use Illuminate\Bus\Queueable; -use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Notification; use Illuminate\Support\Str; use Vinkla\Hashids\Facades\Hashids; use Symfony\Component\Mime\Email; -class FormEmailNotification extends Notification implements ShouldQueue +class FormEmailNotification extends Notification { - use Queueable; - public FormSubmitted $event; - public string $mailer; /** * Create a new notification instance. * * @return void */ - public function __construct(FormSubmitted $event, private $integrationData, string $mailer) + public function __construct(FormSubmitted $event, private $integrationData) { $this->event = $event; - $this->mailer = $mailer; + } + + private function getMailer(): string + { + $workspace = $this->event->form->workspace; + $emailSettings = $workspace->settings['email_settings'] ?? []; + + if ( + $workspace->is_pro + && $emailSettings + && !empty($emailSettings['host']) + && !empty($emailSettings['port']) + && !empty($emailSettings['username']) + && !empty($emailSettings['password']) + ) { + config([ + 'mail.mailers.custom_smtp.host' => $emailSettings['host'], + 'mail.mailers.custom_smtp.port' => $emailSettings['port'], + 'mail.mailers.custom_smtp.username' => $emailSettings['username'], + 'mail.mailers.custom_smtp.password' => $emailSettings['password'] + ]); + return 'custom_smtp'; + } + + return config('mail.default'); } /** @@ -51,7 +70,7 @@ class FormEmailNotification extends Notification implements ShouldQueue public function toMail($notifiable) { return (new MailMessage()) - ->mailer($this->mailer) + ->mailer($this->getMailer()) ->replyTo($this->getReplyToEmail($this->event->form->creator->email)) ->from($this->getFromEmail(), $this->getSenderName()) ->subject($this->getSubject()) diff --git a/api/tests/Feature/Forms/CustomSmtpTest.php b/api/tests/Feature/Forms/CustomSmtpTest.php index d82f8f7d..467791f4 100644 --- a/api/tests/Feature/Forms/CustomSmtpTest.php +++ b/api/tests/Feature/Forms/CustomSmtpTest.php @@ -3,6 +3,7 @@ use App\Notifications\Forms\FormEmailNotification; use Tests\Helpers\FormSubmissionDataFactory; use Illuminate\Notifications\AnonymousNotifiable; +use Illuminate\Support\Facades\Notification; it('can not save custom SMTP settings if not pro user', function () { $user = $this->actingAsUser(); @@ -54,8 +55,12 @@ it('send email with custom SMTP settings', function () { new AnonymousNotifiable(), FormEmailNotification::class, function (FormEmailNotification $notification, $channels, $notifiable) { + $renderedMail = $notification->toMail($notifiable); return $notifiable->routes['mail'] === 'test@test.com' && - $notification->mailer === 'custom_smtp'; + config('mail.mailers.custom_smtp.host') === 'custom.smtp.host' && + config('mail.mailers.custom_smtp.port') === '587' && + config('mail.mailers.custom_smtp.username') === 'custom_username' && + config('mail.mailers.custom_smtp.password') === 'custom_password'; } ); });