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