able to use resend for pro+ users

This commit is contained in:
Andras Bacsai
2023-08-31 15:00:59 +02:00
parent 2538890b52
commit ae8bd69106
25 changed files with 407 additions and 219 deletions

View File

@@ -44,7 +44,7 @@ class DeploymentFailed extends Notification implements ShouldQueue
public function via(object $notifiable): array
{
$channels = [];
$isEmailEnabled = data_get($notifiable, 'smtp_enabled');
$isEmailEnabled = isEmailEnabled($notifiable);
$isDiscordEnabled = data_get($notifiable, 'discord_enabled');
$isSubscribedToEmailEvent = data_get($notifiable, 'smtp_notifications_deployments');
$isSubscribedToDiscordEvent = data_get($notifiable, 'discord_notifications_deployments');

View File

@@ -21,7 +21,7 @@ class EmailChannel
$mailMessage = $notification->toMail($notifiable);
if ($this->isResend) {
foreach($recepients as $receipient) {
foreach ($recepients as $receipient) {
Mail::send(
[],
[],
@@ -35,7 +35,6 @@ class EmailChannel
->html((string)$mailMessage->render())
);
}
} else {
Mail::send(
[],
@@ -50,22 +49,26 @@ class EmailChannel
->html((string)$mailMessage->render())
);
}
}
private function bootConfigs($notifiable): void
{
if (data_get($notifiable, 'resend_enabled')) {
$resendAPIKey = data_get($notifiable, 'resend_api_key');
if ($resendAPIKey) {
$this->isResend = true;
config()->set('mail.default', 'resend');
config()->set('resend.api_key', $resendAPIKey);
if (data_get($notifiable, 'use_instance_email_settings')) {
$type = set_transanctional_email_settings();
if (!$type) {
throw new Exception('No email settings found.');
}
if ($type === 'resend') {
$this->isResend = true;
}
return;
}
if (data_get($notifiable, 'resend_enabled')) {
$this->isResend = true;
config()->set('mail.default', 'resend');
config()->set('resend.api_key', data_get($notifiable, 'resend_api_key'));
}
if (data_get($notifiable, 'smtp_enabled')) {
$password = data_get($notifiable, 'smtp_password');
if ($password) $password = decrypt($password);
config()->set('mail.default', 'smtp');
config()->set('mail.mailers.smtp', [
"transport" => "smtp",
@@ -73,7 +76,7 @@ class EmailChannel
"port" => data_get($notifiable, 'smtp_port'),
"encryption" => data_get($notifiable, 'smtp_encryption'),
"username" => data_get($notifiable, 'smtp_username'),
"password" => $password,
"password" => data_get($notifiable, 'smtp_password'),
"timeout" => data_get($notifiable, 'smtp_timeout'),
"local_domain" => null,
]);

View File

@@ -4,16 +4,20 @@ namespace App\Notifications\Channels;
use App\Models\InstanceSettings;
use App\Models\User;
use Exception;
use Illuminate\Mail\Message;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Mail;
use Log;
class TransactionalEmailChannel
{
private bool $isResend = false;
public function send(User $notifiable, Notification $notification): void
{
$settings = InstanceSettings::get();
if (data_get($settings, 'smtp_enabled') !== true) {
if (!data_get($settings, 'smtp_enabled') && !data_get($settings, 'resend_enabled')) {
Log::info('SMTP/Resend not enabled');
return;
}
$email = $notifiable->email;
@@ -22,22 +26,43 @@ class TransactionalEmailChannel
}
$this->bootConfigs();
$mailMessage = $notification->toMail($notifiable);
Mail::send(
[],
[],
fn (Message $message) => $message
->from(
data_get($settings, 'smtp_from_address'),
data_get($settings, 'smtp_from_name')
)
->to($email)
->subject($mailMessage->subject)
->html((string)$mailMessage->render())
);
if ($this->isResend) {
Mail::send(
[],
[],
fn (Message $message) => $message
->from(
data_get($settings, 'smtp_from_address'),
data_get($settings, 'smtp_from_name'),
)
->to($email)
->subject($mailMessage->subject)
->html((string)$mailMessage->render())
);
} else {
Mail::send(
[],
[],
fn (Message $message) => $message
->from(
data_get($settings, 'smtp_from_address'),
data_get($settings, 'smtp_from_name'),
)
->bcc($email)
->subject($mailMessage->subject)
->html((string)$mailMessage->render())
);
}
}
private function bootConfigs(): void
{
set_transanctional_email_settings();
$type = set_transanctional_email_settings();
if (!$type) {
throw new Exception('No email settings found.');
}
if ($type === 'resend') {
$this->isResend = true;
}
}
}

View File

@@ -25,7 +25,7 @@ class BackupFailed extends Notification implements ShouldQueue
public function via(object $notifiable): array
{
$channels = [];
$isEmailEnabled = data_get($notifiable, 'smtp_enabled');
$isEmailEnabled = isEmailEnabled($notifiable);
$isDiscordEnabled = data_get($notifiable, 'discord_enabled');
$isSubscribedToEmailEvent = data_get($notifiable, 'smtp_notifications_database_backups');
$isSubscribedToDiscordEvent = data_get($notifiable, 'discord_notifications_database_backups');

View File

@@ -25,7 +25,7 @@ class BackupSuccess extends Notification implements ShouldQueue
public function via(object $notifiable): array
{
$channels = [];
$isEmailEnabled = data_get($notifiable, 'smtp_enabled');
$isEmailEnabled = isEmailEnabled($notifiable);
$isDiscordEnabled = data_get($notifiable, 'discord_enabled');
$isSubscribedToEmailEvent = data_get($notifiable, 'smtp_notifications_database_backups');
$isSubscribedToDiscordEvent = data_get($notifiable, 'discord_notifications_database_backups');

View File

@@ -23,7 +23,7 @@ class NotReachable extends Notification implements ShouldQueue
public function via(object $notifiable): array
{
$channels = [];
$isEmailEnabled = data_get($notifiable, 'smtp_enabled');
$isEmailEnabled = isEmailEnabled($notifiable);
$isDiscordEnabled = data_get($notifiable, 'discord_enabled');
$isSubscribedToEmailEvent = data_get($notifiable, 'smtp_notifications_status_changes');
$isSubscribedToDiscordEvent = data_get($notifiable, 'discord_notifications_status_changes');

View File

@@ -20,7 +20,7 @@ class Test extends Notification implements ShouldQueue
public function via(object $notifiable): array
{
$channels = [];
$isEmailEnabled = data_get($notifiable, 'smtp_enabled');
$isEmailEnabled = isEmailEnabled($notifiable);
$isDiscordEnabled = data_get($notifiable, 'discord_enabled');
if ($isDiscordEnabled && empty($this->emails)) {

View File

@@ -31,24 +31,11 @@ class ResetPassword extends Notification
public function via($notifiable)
{
if ($this->settings->smtp_enabled) {
$password = data_get($this->settings, 'smtp_password');
if ($password) $password = decrypt($password);
config()->set('mail.default', 'smtp');
config()->set('mail.mailers.smtp', [
"transport" => "smtp",
"host" => data_get($this->settings, 'smtp_host'),
"port" => data_get($this->settings, 'smtp_port'),
"encryption" => data_get($this->settings, 'smtp_encryption'),
"username" => data_get($this->settings, 'smtp_username'),
"password" => $password,
"timeout" => data_get($this->settings, 'smtp_timeout'),
"local_domain" => null,
]);
return ['mail'];
$type = set_transanctional_email_settings();
if (!$type) {
throw new \Exception('No email settings found.');
}
throw new \Exception('SMTP is not enabled');
return ['mail'];
}
public function toMail($notifiable)