Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

test transactional emails

Andras Bacsai5d864f588833
diff --git a/app/Http/Livewire/Notifications/EmailSettings.php b/app/Http/Livewire/Notifications/EmailSettings.php
index 73d7e6c4df..a8240ac706 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 75936d6297..42a1c70004 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,
- ]);
-
- $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()
- {
+ 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()
+ // {
+ // }
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 c2394a008b..9a3ba1a753 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 0a703199ad..a1d7c7c130 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 d2d64788ae..c4e8eeccdc 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 0000000000..685cbea7d3
--- /dev/null
+++ b/app/Notifications/TestTransactionEmail.php
@@ -0,0 +1,52 @@
+<?php
+
+namespace App\Notifications;
+
+use App\Notifications\Channels\EmailChannel;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Notifications\Messages\MailMessage;
+use Illuminate\Notifications\Notification;
+
+class TestTransactionEmail extends Notification implements ShouldQueue
+{
+ use Queueable;
+
+ /**
+ * Create a new notification instance.
+ */
+ public function __construct()
+ {
+ //
+ }
+
+ /**
+ * Get the notification's delivery channels.
+ *
+ * @return array<int, string>
+ */
+ 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 f4445e6588..2432d02442 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 0000000000..3ad0eb0b0c
--- /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 ae69de13ec..366f510409 100644
--- a/resources/views/livewire/notifications/email-settings.blade.php
+++ b/resources/views/livewire/notifications/email-settings.blade.php
@@ -11,10 +11,9 @@
</div>
<div class="flex gap-2">
- <x-forms.input required id="model.extra_attributes.recipients"
+ <x-forms.input required id="model.extra_attributes.smtp_recipients"
helper="Email list to send the all notifications to, separated by comma." label="Recipient(s)" />
- <x-forms.input id="model.extra_attributes.test_notification_recipients"
- label="Test Notification Recipient(s)"
+ <x-forms.input id="model.extra_attributes.smtp_test_recipients" label="Test Notification Recipient(s)"
helper="Email list to send the test notification to, separated by comma." />
</div>
<div class="flex flex-col gap-2 xl:flex-row">
@@ -34,10 +33,10 @@
label="Timeout" />
</div>
<div class="flex flex-col w-96">
- <x-forms.input required id="model.extra_attributes.from_name" helper="Name used in emails."
+ <x-forms.input required id="model.extra_attributes.smtp_from_name" helper="Name used in emails."
label="From Name" />
- <x-forms.input required id="model.extra_attributes.from_address" helper="Email address used in emails."
- label="From Address" />
+ <x-forms.input required id="model.extra_attributes.smtp_from_address"
+ helper="Email address used in emails." label="From Address" />
</div>
</div>
</form>
diff --git a/resources/views/livewire/settings/email.blade.php b/resources/views/livewire/settings/email.blade.php
index aada159d7e..d7cb4b8194 100644
--- a/resources/views/livewire/settings/email.blade.php
+++ b/resources/views/livewire/settings/email.blade.php
@@ -8,34 +8,38 @@
</div>
<div class="pt-2 pb-4 text-sm">SMTP settings for password reset, invitation, etc.</div>
<div class="flex items-end gap-2">
- <x-forms.input required id="settings.smtp_recipients"
+ <x-forms.input required id="settings.extra_attributes.smtp_recipients"
helper="Email list to send the all notifications to, separated by comma." label="Recipient(s)" />
- <x-forms.input id="settings.smtp_test_recipients" label="Test Recipient(s)"
+ <x-forms.input id="settings.extra_attributes.smtp_test_recipients" label="Test Recipient(s)"
helper="Email list to send a test email to, separated by comma." />
- <x-forms.button wire:click='test_email_local'>
+ {{-- <x-forms.button wire:click='test_email_local'>
Send Test Email (local Mailpit)
- </x-forms.button>
+ </x-forms.button> --}}
<x-forms.button wire:click='test_email'>
Send Test Email (SMTP)
</x-forms.button>
</div>
<div class="flex flex-col gap-2 xl:flex-row">
<div class="flex flex-col w-96">
- <x-forms.input required id="settings.smtp_host" helper="SMTP Hostname" placeholder="smtp.mailgun.org"
- label="Host" />
- <x-forms.input required id="settings.smtp_port" helper="SMTP Port" placeholder="587" label="Port" />
- <x-forms.input id="settings.smtp_encryption" helper="If SMTP through SSL, set it to 'tls'."
- placeholder="tls" label="Encryption" />
+ <x-forms.input required id="settings.extra_attributes.smtp_host" helper="SMTP Hostname"
+ placeholder="smtp.mailgun.org" label="Host" />
+ <x-forms.input required id="settings.extra_attributes.smtp_port" helper="SMTP Port" placeholder="587"
+ label="Port" />
+ <x-forms.input id="settings.extra_attributes.smtp_encryption"
+ helper="If SMTP through SSL, set it to 'tls'." placeholder="tls" label="Encryption" />
</div>
<div class="flex flex-col w-96">
- <x-forms.input id="settings.smtp_username" helper="SMTP Username" label="Username" />
- <x-forms.input id="settings.smtp_password" type="password" helper="SMTP Password" label="Password" />
- <x-forms.input id="settings.smtp_timeout" helper="Timeout value for sending emails." label="Timeout" />
+ <x-forms.input id="settings.extra_attributes.smtp_username" helper="SMTP Username" label="Username" />
+ <x-forms.input id="settings.extra_attributes.smtp_password" type="password" helper="SMTP Password"
+ label="Password" />
+ <x-forms.input id="settings.extra_attributes.smtp_timeout" helper="Timeout value for sending emails."
+ label="Timeout" />
</div>
<div class="flex flex-col w-96">
- <x-forms.input required id="settings.smtp_from_name" helper="Name used in emails." label="From Name" />
- <x-forms.input required id="settings.smtp_from_address" helper="Email address used in emails."
- label="From Address" />
+ <x-forms.input required id="settings.extra_attributes.smtp_from_name" helper="Name used in emails."
+ label="From Name" />
+ <x-forms.input required id="settings.extra_attributes.smtp_from_address"
+ helper="Email address used in emails." label="From Address" />
</div>
</div>
</form>