refactor(email): streamline email sending logic and improve configuration handling
This commit is contained in:
@@ -2,102 +2,63 @@
|
|||||||
|
|
||||||
namespace App\Notifications\Channels;
|
namespace App\Notifications\Channels;
|
||||||
|
|
||||||
use App\Models\Team;
|
|
||||||
use App\Services\ConfigurationRepository;
|
|
||||||
use Exception;
|
|
||||||
use Illuminate\Mail\Message;
|
|
||||||
use Illuminate\Notifications\Notification;
|
use Illuminate\Notifications\Notification;
|
||||||
use Illuminate\Support\Facades\Mail;
|
use Resend;
|
||||||
|
|
||||||
class EmailChannel
|
class EmailChannel
|
||||||
{
|
{
|
||||||
private ConfigurationRepository $configRepository;
|
public function __construct() {}
|
||||||
|
|
||||||
public function __construct(ConfigurationRepository $configRepository)
|
|
||||||
{
|
|
||||||
$this->configRepository = $configRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function send(SendsEmail $notifiable, Notification $notification): void
|
public function send(SendsEmail $notifiable, Notification $notification): void
|
||||||
{
|
{
|
||||||
try {
|
$useInstanceEmailSettings = $notifiable->emailNotificationSettings->use_instance_email_settings;
|
||||||
$team = data_get($notifiable, 'id');
|
if ($useInstanceEmailSettings) {
|
||||||
$members = Team::find($team)->members;
|
$settings = instanceSettings();
|
||||||
$mailerType = $this->bootConfigs($notifiable);
|
} else {
|
||||||
|
$settings = $notifiable->emailNotificationSettings;
|
||||||
|
}
|
||||||
|
$isResendEnabled = $settings->resend_enabled;
|
||||||
|
$isSmtpEnabled = $settings->smtp_enabled;
|
||||||
|
$recipients = $notifiable->getRecipients();
|
||||||
|
$mailMessage = $notification->toMail($notifiable);
|
||||||
|
|
||||||
$recipients = $notifiable->getRecipients();
|
if ($isResendEnabled) {
|
||||||
if (count($recipients) === 0) {
|
$resend = Resend::client($settings->resend_api_key);
|
||||||
throw new Exception('No email recipients found');
|
$resend->emails->send([
|
||||||
}
|
'from' => $settings->smtp_from_address,
|
||||||
foreach ($recipients as $recipient) {
|
'to' => $recipients,
|
||||||
// check if the recipient is part of the team
|
'subject' => $mailMessage->subject,
|
||||||
if (! $members->contains('email', $recipient)) {
|
'html' => (string) $mailMessage->render(),
|
||||||
$emailSettings = $notifiable->emailNotificationSettings;
|
]);
|
||||||
data_set($emailSettings, 'smtp_password', '********');
|
} elseif ($isSmtpEnabled) {
|
||||||
data_set($emailSettings, 'resend_api_key', '********');
|
$encryption = match (strtolower($settings->smtp_encryption)) {
|
||||||
send_internal_notification(sprintf(
|
'starttls' => null,
|
||||||
"Recipient is not part of the team: %s\nTeam: %s\nNotification: %s\nNotifiable: %s\nMailer Type: %s\nEmail Settings:\n%s",
|
'tls' => 'tls',
|
||||||
$recipient,
|
'none' => null,
|
||||||
$team,
|
default => null,
|
||||||
get_class($notification),
|
};
|
||||||
get_class($notifiable),
|
|
||||||
$mailerType,
|
|
||||||
json_encode($emailSettings, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
|
|
||||||
));
|
|
||||||
throw new Exception('Recipient is not part of the team');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$mailMessage = $notification->toMail($notifiable);
|
$transport = new \Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport(
|
||||||
|
$settings->smtp_host,
|
||||||
Mail::mailer($mailerType)->send(
|
$settings->smtp_port,
|
||||||
[],
|
$encryption
|
||||||
[],
|
|
||||||
fn (Message $message) => $message
|
|
||||||
->to($recipients)
|
|
||||||
->subject($mailMessage->subject)
|
|
||||||
->html((string) $mailMessage->render())
|
|
||||||
);
|
);
|
||||||
} catch (Exception $e) {
|
$transport->setUsername($settings->smtp_username);
|
||||||
$error = $e->getMessage();
|
$transport->setPassword($settings->smtp_password);
|
||||||
if ($error === 'No email settings found.') {
|
|
||||||
throw $e;
|
$mailer = new \Symfony\Component\Mailer\Mailer($transport);
|
||||||
}
|
|
||||||
$message = "EmailChannel error: {$e->getMessage()}. Failed to send email to:";
|
$fromEmail = $settings->smtp_from_address ?? 'noreply@localhost';
|
||||||
if (isset($recipients)) {
|
$fromName = $settings->smtp_from_name ?? 'System';
|
||||||
$message .= implode(', ', $recipients);
|
$from = new \Symfony\Component\Mime\Address($fromEmail, $fromName);
|
||||||
}
|
|
||||||
if (isset($mailMessage)) {
|
$email = (new \Symfony\Component\Mime\Email)
|
||||||
$message .= " with subject: {$mailMessage->subject}";
|
->from($from)
|
||||||
}
|
->to(...$recipients)
|
||||||
send_internal_notification($message);
|
->subject($mailMessage->subject)
|
||||||
throw $e;
|
->html((string) $mailMessage->render());
|
||||||
|
|
||||||
|
$mailer->send($email);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function bootConfigs($notifiable): string
|
|
||||||
{
|
|
||||||
$emailSettings = $notifiable->emailNotificationSettings;
|
|
||||||
|
|
||||||
if ($emailSettings->use_instance_email_settings) {
|
|
||||||
$type = set_transanctional_email_settings();
|
|
||||||
if (blank($type)) {
|
|
||||||
throw new Exception('No email settings found.');
|
|
||||||
}
|
|
||||||
|
|
||||||
return $type;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->configRepository->updateMailConfig($emailSettings);
|
|
||||||
|
|
||||||
if ($emailSettings->resend_enabled) {
|
|
||||||
return 'resend';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($emailSettings->smtp_enabled) {
|
|
||||||
return 'smtp';
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Exception('No email settings found.');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user