Skip to content

Commit

Permalink
Refactor Email Integration Handling
Browse files Browse the repository at this point in the history
- Removed the AbstractEmailIntegrationHandler class to streamline email integration logic.
- Updated EmailIntegration class to extend AbstractIntegrationHandler instead of the removed class.
- Modified FormEmailNotification to handle mailer configuration internally, eliminating the need to pass the mailer as a parameter.

These changes enhance the clarity and maintainability of the email integration process by consolidating configuration logic and reducing class dependencies.
  • Loading branch information
JhumanJ committed Jan 14, 2025
1 parent 06c3512 commit 6e1e0b1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 55 deletions.
47 changes: 0 additions & 47 deletions api/app/Integrations/Handlers/AbstractEmailIntegrationHandler.php

This file was deleted.

7 changes: 3 additions & 4 deletions api/app/Integrations/Handlers/EmailIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use App\Service\Forms\FormSubmissionFormatter;
use Illuminate\Validation\ValidationException;

class EmailIntegration extends AbstractEmailIntegrationHandler
class EmailIntegration extends AbstractIntegrationHandler
{
public const RISKY_USERS_LIMIT = 120;

Expand Down Expand Up @@ -95,16 +95,15 @@ public function handle(): void
->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)
);
});
}
Expand Down
31 changes: 27 additions & 4 deletions api/app/Notifications/Forms/FormEmailNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,40 @@ class FormEmailNotification extends Notification implements ShouldQueue
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');
}

/**
Expand All @@ -51,7 +74,7 @@ public function via($notifiable)
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())
Expand Down

0 comments on commit 6e1e0b1

Please sign in to comment.