From 5d864f58883343bd22dec6b51df63d0155a91978 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 6 Jun 2023 17:50:13 +0200 Subject: [PATCH] test transactional emails --- .../Livewire/Notifications/EmailSettings.php | 20 ++--- app/Http/Livewire/Settings/Email.php | 78 ++++++++++--------- app/Models/InstanceSettings.php | 22 +++++- app/Models/Team.php | 2 +- app/Notifications/Channels/EmailChannel.php | 11 +-- app/Notifications/TestTransactionEmail.php | 52 +++++++++++++ ..._112813_create_instance_settings_table.php | 14 +--- resources/views/emails/test-email.blade.php | 1 + .../notifications/email-settings.blade.php | 11 ++- .../views/livewire/settings/email.blade.php | 34 ++++---- 10 files changed, 156 insertions(+), 89 deletions(-) create mode 100644 app/Notifications/TestTransactionEmail.php create mode 100644 resources/views/emails/test-email.blade.php diff --git a/app/Http/Livewire/Notifications/EmailSettings.php b/app/Http/Livewire/Notifications/EmailSettings.php index 73d7e6c4d..a8240ac70 100644 --- a/app/Http/Livewire/Notifications/EmailSettings.php +++ b/app/Http/Livewire/Notifications/EmailSettings.php @@ -11,34 +11,34 @@ class EmailSettings extends Component protected $rules = [ 'model.extra_attributes.smtp_active' => 'nullable|boolean', - 'model.extra_attributes.from_address' => 'required|email', - 'model.extra_attributes.from_name' => 'required', - 'model.extra_attributes.recipients' => 'required', + 'model.extra_attributes.smtp_from_address' => 'required|email', + 'model.extra_attributes.smtp_from_name' => 'required', + 'model.extra_attributes.smtp_recipients' => 'required', 'model.extra_attributes.smtp_host' => 'required', 'model.extra_attributes.smtp_port' => 'required', 'model.extra_attributes.smtp_encryption' => 'nullable', 'model.extra_attributes.smtp_username' => 'nullable', 'model.extra_attributes.smtp_password' => 'nullable', 'model.extra_attributes.smtp_timeout' => 'nullable', - 'model.extra_attributes.test_notification_recipients' => 'nullable', + 'model.extra_attributes.smtp_test_recipients' => 'nullable', ]; protected $validationAttributes = [ - 'model.extra_attributes.from_address' => '', - 'model.extra_attributes.from_name' => '', - 'model.extra_attributes.recipients' => '', + 'model.extra_attributes.smtp_from_address' => '', + 'model.extra_attributes.smtp_from_name' => '', + 'model.extra_attributes.smtp_recipients' => '', 'model.extra_attributes.smtp_host' => '', 'model.extra_attributes.smtp_port' => '', 'model.extra_attributes.smtp_encryption' => '', 'model.extra_attributes.smtp_username' => '', 'model.extra_attributes.smtp_password' => '', - 'model.extra_attributes.test_notification_recipients' => '', + 'model.extra_attributes.smtp_test_recipients' => '', ]; public function submit() { $this->resetErrorBag(); $this->validate(); - $this->model->extra_attributes->recipients = str_replace(' ', '', $this->model->extra_attributes->recipients); - $this->model->extra_attributes->test_notification_recipients = str_replace(' ', '', $this->model->extra_attributes->test_notification_recipients); + $this->model->extra_attributes->smtp_recipients = str_replace(' ', '', $this->model->extra_attributes->smtp_recipients); + $this->model->extra_attributes->smtp_test_recipients = str_replace(' ', '', $this->model->extra_attributes->smtp_test_recipients); $this->saveModel(); } private function saveModel() diff --git a/app/Http/Livewire/Settings/Email.php b/app/Http/Livewire/Settings/Email.php index 75936d629..42a1c7000 100644 --- a/app/Http/Livewire/Settings/Email.php +++ b/app/Http/Livewire/Settings/Email.php @@ -4,7 +4,9 @@ namespace App\Http\Livewire\Settings; use App\Mail\TestTransactionalEmail; use App\Models\InstanceSettings; +use App\Notifications\TestTransactionEmail; use Illuminate\Support\Facades\Mail; +use Illuminate\Support\Facades\Notification; use Livewire\Component; class Email extends Component @@ -12,49 +14,53 @@ class Email extends Component public InstanceSettings $settings; protected $rules = [ - 'settings.smtp_host' => 'required', - 'settings.smtp_port' => 'required|numeric', - 'settings.smtp_encryption' => 'nullable', - 'settings.smtp_username' => 'nullable', - 'settings.smtp_password' => 'nullable', - 'settings.smtp_timeout' => 'nullable', - 'settings.smtp_recipients' => 'required', - 'settings.smtp_test_recipients' => 'nullable', - 'settings.smtp_from_address' => 'required|email', - 'settings.smtp_from_name' => 'required', + 'settings.extra_attributes.smtp_host' => 'required', + 'settings.extra_attributes.smtp_port' => 'required|numeric', + 'settings.extra_attributes.smtp_encryption' => 'nullable', + 'settings.extra_attributes.smtp_username' => 'nullable', + 'settings.extra_attributes.smtp_password' => 'nullable', + 'settings.extra_attributes.smtp_timeout' => 'nullable', + 'settings.extra_attributes.smtp_recipients' => 'required', + 'settings.extra_attributes.smtp_test_recipients' => 'nullable', + 'settings.extra_attributes.smtp_from_address' => 'required|email', + 'settings.extra_attributes.smtp_from_name' => 'required', ]; public function test_email() { - config()->set('mail.default', 'smtp'); - config()->set('mail.mailers.smtp', [ - "transport" => "smtp", - "host" => $this->settings->smtp_host, - "port" => $this->settings->smtp_port, - "encryption" => $this->settings->smtp_encryption, - "username" => $this->settings->smtp_username, - "password" => $this->settings->smtp_password, - ]); + Notification::send($this->settings, new TestTransactionEmail); + } + // public function test_email() + // { + // config()->set('mail.default', 'smtp'); + // config()->set('mail.mailers.smtp', [ + // "transport" => "smtp", + // "host" => $this->settings->smtp_host, + // "port" => $this->settings->smtp_port, + // "encryption" => $this->settings->smtp_encryption, + // "username" => $this->settings->smtp_username, + // "password" => $this->settings->smtp_password, + // ]); - $this->send_email(); - } - public function test_email_local() - { - config()->set('mail.default', 'smtp'); - config()->set('mail.mailers.smtp', [ - "transport" => "smtp", - "host" => 'coolify-mail', - "port" => 1025, - ]); - $this->send_email(); - } - private function send_email() - { - } + // $this->send_email(); + // } + // public function test_email_local() + // { + // config()->set('mail.default', 'smtp'); + // config()->set('mail.mailers.smtp', [ + // "transport" => "smtp", + // "host" => 'coolify-mail', + // "port" => 1025, + // ]); + // $this->send_email(); + // } + // private function send_email() + // { + // } public function submit() { $this->validate(); - $this->settings->smtp_recipients = str_replace(' ', '', $this->settings->smtp_recipients); - $this->settings->smtp_test_recipients = str_replace(' ', '', $this->settings->smtp_test_recipients); + $this->settings->extra_attributes->smtp_recipients = str_replace(' ', '', $this->settings->extra_attributes->smtp_recipients); + $this->settings->extra_attributes->smtp_test_recipients = str_replace(' ', '', $this->settings->extra_attributes->smtp_test_recipients); $this->settings->save(); } } diff --git a/app/Models/InstanceSettings.php b/app/Models/InstanceSettings.php index c2394a008..9a3ba1a75 100644 --- a/app/Models/InstanceSettings.php +++ b/app/Models/InstanceSettings.php @@ -2,10 +2,30 @@ namespace App\Models; +use App\Notifications\Channels\SendsEmail; +use Illuminate\Contracts\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; +use Illuminate\Notifications\Notifiable; +use Spatie\SchemalessAttributes\Casts\SchemalessAttributes; -class InstanceSettings extends Model +class InstanceSettings extends Model implements SendsEmail { + use Notifiable; + protected $casts = [ + 'extra_attributes' => SchemalessAttributes::class, + ]; + public function scopeWithExtraAttributes(): Builder + { + return $this->extra_attributes->modelScope(); + } + public function routeNotificationForEmail(string $attribute = 'smtp_recipients') + { + $recipients = $this->extra_attributes->get($attribute, ''); + if (is_null($recipients) || $recipients === '') { + return []; + } + return explode(',', $recipients); + } public static function get() { return InstanceSettings::findOrFail(0); diff --git a/app/Models/Team.php b/app/Models/Team.php index 0a703199a..a1d7c7c13 100644 --- a/app/Models/Team.php +++ b/app/Models/Team.php @@ -27,7 +27,7 @@ class Team extends BaseModel implements SendsDiscord, SendsEmail { return $this->extra_attributes->get('discord_webhook'); } - public function routeNotificationForEmail(string $attribute = 'recipients') + public function routeNotificationForEmail(string $attribute = 'smtp_recipients') { $recipients = $this->extra_attributes->get($attribute, ''); if (is_null($recipients) || $recipients === '') { diff --git a/app/Notifications/Channels/EmailChannel.php b/app/Notifications/Channels/EmailChannel.php index d2d64788a..c4e8eeccd 100644 --- a/app/Notifications/Channels/EmailChannel.php +++ b/app/Notifications/Channels/EmailChannel.php @@ -8,14 +8,12 @@ use Illuminate\Support\Facades\Mail; class EmailChannel { - /** - * Send the given notification. - */ public function send(SendsEmail $notifiable, Notification $notification): void { $this->bootConfigs($notifiable); + if ($notification instanceof \App\Notifications\TestNotification) { - $bcc = $notifiable->routeNotificationForEmail('test_notification_recipients'); + $bcc = $notifiable->routeNotificationForEmail('smtp_test_recipients'); if (count($bcc) === 0) { $bcc = $notifiable->routeNotificationForEmail(); } @@ -29,10 +27,9 @@ class EmailChannel [], fn (Message $message) => $message ->from( - $notifiable->extra_attributes?->get('from_address'), - $notifiable->extra_attributes?->get('from_name') + $notifiable->extra_attributes?->get('smtp_from_address'), + $notifiable->extra_attributes?->get('smtp_from_name') ) - ->cc($bcc) ->bcc($bcc) ->subject($mailMessage->subject) ->html((string)$mailMessage->render()) diff --git a/app/Notifications/TestTransactionEmail.php b/app/Notifications/TestTransactionEmail.php new file mode 100644 index 000000000..685cbea7d --- /dev/null +++ b/app/Notifications/TestTransactionEmail.php @@ -0,0 +1,52 @@ + + */ + public function via(object $notifiable): array + { + $channels = []; + $notifiable->extra_attributes?->get('smtp_host') && $channels[] = EmailChannel::class; + return $channels; + } + + /** + * Get the mail representation of the notification. + */ + public function toMail(object $notifiable): MailMessage + { + $mail = new MailMessage(); + $mail->subject('Coolify Test Notification'); + $mail->view('emails.test-email'); + return $mail; + } + + public function toArray(object $notifiable): array + { + return [ + // + ]; + } +} diff --git a/database/migrations/2023_03_20_112813_create_instance_settings_table.php b/database/migrations/2023_03_20_112813_create_instance_settings_table.php index f4445e658..2432d0244 100644 --- a/database/migrations/2023_03_20_112813_create_instance_settings_table.php +++ b/database/migrations/2023_03_20_112813_create_instance_settings_table.php @@ -21,19 +21,7 @@ return new class extends Migration $table->boolean('do_not_track')->default(false); $table->boolean('is_auto_update_enabled')->default(true); $table->boolean('is_registration_enabled')->default(true); - - // SMTP for transactional emails - $table->string('smtp_host')->nullable(); - $table->integer('smtp_port')->nullable(); - $table->string('smtp_encryption')->nullable(); - $table->string('smtp_username')->nullable(); - $table->string('smtp_password')->nullable(); - $table->integer('smtp_timeout')->nullable(); - $table->string('smtp_from_address')->nullable(); - $table->string('smtp_from_name')->nullable(); - $table->string('smtp_test_recipients')->nullable(); - $table->string('smtp_recipients')->nullable(); - + $table->schemalessAttributes('extra_attributes'); // $table->string('custom_dns_servers')->default('1.1.1.1,8.8.8.8'); // $table->boolean('is_dns_check_enabled')->default(true); $table->timestamps(); diff --git a/resources/views/emails/test-email.blade.php b/resources/views/emails/test-email.blade.php new file mode 100644 index 000000000..3ad0eb0b0 --- /dev/null +++ b/resources/views/emails/test-email.blade.php @@ -0,0 +1 @@ +Hello from test email. diff --git a/resources/views/livewire/notifications/email-settings.blade.php b/resources/views/livewire/notifications/email-settings.blade.php index ae69de13e..366f51040 100644 --- a/resources/views/livewire/notifications/email-settings.blade.php +++ b/resources/views/livewire/notifications/email-settings.blade.php @@ -11,10 +11,9 @@
- -
@@ -34,10 +33,10 @@ label="Timeout" />
- - +
diff --git a/resources/views/livewire/settings/email.blade.php b/resources/views/livewire/settings/email.blade.php index aada159d7..d7cb4b819 100644 --- a/resources/views/livewire/settings/email.blade.php +++ b/resources/views/livewire/settings/email.blade.php @@ -8,34 +8,38 @@
SMTP settings for password reset, invitation, etc.
- - - + {{-- Send Test Email (local Mailpit) - + --}} Send Test Email (SMTP)
- - - + + +
- - - + + +
- - + +