test transactional emails

This commit is contained in:
Andras Bacsai 2023-06-06 17:50:13 +02:00
parent 05ee63cc83
commit 5d864f5888
10 changed files with 156 additions and 89 deletions

View File

@ -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()

View File

@ -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();
}
}

View File

@ -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);

View File

@ -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 === '') {

View File

@ -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())

View File

@ -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 [
//
];
}
}

View File

@ -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();

View File

@ -0,0 +1 @@
Hello from test email.

View File

@ -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>

View File

@ -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>