2023-06-06 13:30:33 +00:00
|
|
|
<?php
|
|
|
|
|
2023-12-07 18:06:32 +00:00
|
|
|
namespace App\Livewire\Settings;
|
2023-06-06 13:30:33 +00:00
|
|
|
|
|
|
|
use App\Models\InstanceSettings;
|
2023-07-28 08:55:26 +00:00
|
|
|
use App\Notifications\TransactionalEmails\Test;
|
2023-06-06 13:30:33 +00:00
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
class Email extends Component
|
|
|
|
{
|
|
|
|
public InstanceSettings $settings;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-07-28 08:55:26 +00:00
|
|
|
public string $emails;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-06-06 13:30:33 +00:00
|
|
|
protected $rules = [
|
2023-07-27 19:26:15 +00:00
|
|
|
'settings.smtp_enabled' => 'nullable|boolean',
|
|
|
|
'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_from_address' => 'required|email',
|
|
|
|
'settings.smtp_from_name' => 'required',
|
2023-08-31 11:10:39 +00:00
|
|
|
'settings.resend_enabled' => 'nullable|boolean',
|
2024-06-10 20:43:34 +00:00
|
|
|
'settings.resend_api_key' => 'nullable',
|
2023-08-31 11:10:39 +00:00
|
|
|
|
2023-06-06 13:30:33 +00:00
|
|
|
];
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-06-13 08:51:58 +00:00
|
|
|
protected $validationAttributes = [
|
2023-07-27 19:26:15 +00:00
|
|
|
'settings.smtp_from_address' => 'From Address',
|
|
|
|
'settings.smtp_from_name' => 'From Name',
|
|
|
|
'settings.smtp_recipients' => 'Recipients',
|
|
|
|
'settings.smtp_host' => 'Host',
|
|
|
|
'settings.smtp_port' => 'Port',
|
|
|
|
'settings.smtp_encryption' => 'Encryption',
|
|
|
|
'settings.smtp_username' => 'Username',
|
|
|
|
'settings.smtp_password' => 'Password',
|
2023-08-31 11:10:39 +00:00
|
|
|
'settings.smtp_timeout' => 'Timeout',
|
2024-06-10 20:43:34 +00:00
|
|
|
'settings.resend_api_key' => 'Resend API Key',
|
2023-06-13 08:51:58 +00:00
|
|
|
];
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-06-20 19:18:14 +00:00
|
|
|
public function mount()
|
|
|
|
{
|
2023-07-28 08:55:26 +00:00
|
|
|
$this->emails = auth()->user()->email;
|
2023-06-20 19:18:14 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2024-06-10 20:43:34 +00:00
|
|
|
public function submitFromFields()
|
|
|
|
{
|
2023-08-31 11:10:39 +00:00
|
|
|
try {
|
|
|
|
$this->resetErrorBag();
|
|
|
|
$this->validate([
|
|
|
|
'settings.smtp_from_address' => 'required|email',
|
|
|
|
'settings.smtp_from_name' => 'required',
|
|
|
|
]);
|
|
|
|
$this->settings->save();
|
2024-02-22 13:53:42 +00:00
|
|
|
$this->dispatch('success', 'Settings saved.');
|
2023-09-11 15:36:30 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-09-15 13:34:25 +00:00
|
|
|
return handleError($e, $this);
|
2023-08-31 11:10:39 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
|
|
|
public function submitResend()
|
|
|
|
{
|
2023-08-31 11:10:39 +00:00
|
|
|
try {
|
|
|
|
$this->resetErrorBag();
|
|
|
|
$this->validate([
|
2024-03-27 10:28:12 +00:00
|
|
|
'settings.smtp_from_address' => 'required|email',
|
|
|
|
'settings.smtp_from_name' => 'required',
|
2024-06-10 20:43:34 +00:00
|
|
|
'settings.resend_api_key' => 'required',
|
2023-08-31 11:10:39 +00:00
|
|
|
]);
|
|
|
|
$this->settings->save();
|
2024-02-22 13:53:42 +00:00
|
|
|
$this->dispatch('success', 'Settings saved.');
|
2023-09-11 15:36:30 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-08-31 11:10:39 +00:00
|
|
|
$this->settings->resend_enabled = false;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-15 13:34:25 +00:00
|
|
|
return handleError($e, $this);
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
|
|
|
public function instantSaveResend()
|
|
|
|
{
|
2023-09-08 14:56:14 +00:00
|
|
|
try {
|
|
|
|
$this->settings->smtp_enabled = false;
|
|
|
|
$this->submitResend();
|
2023-09-11 15:36:30 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-09-15 13:34:25 +00:00
|
|
|
return handleError($e, $this);
|
2023-09-08 14:56:14 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-06-13 08:51:58 +00:00
|
|
|
public function instantSave()
|
|
|
|
{
|
|
|
|
try {
|
2023-09-08 14:56:14 +00:00
|
|
|
$this->settings->resend_enabled = false;
|
2023-06-13 08:51:58 +00:00
|
|
|
$this->submit();
|
2023-09-11 15:36:30 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-09-15 13:34:25 +00:00
|
|
|
return handleError($e, $this);
|
2023-06-13 08:51:58 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-06-06 13:30:33 +00:00
|
|
|
public function submit()
|
|
|
|
{
|
2023-08-31 11:10:39 +00:00
|
|
|
try {
|
|
|
|
$this->resetErrorBag();
|
|
|
|
$this->validate([
|
2024-03-27 10:28:12 +00:00
|
|
|
'settings.smtp_from_address' => 'required|email',
|
|
|
|
'settings.smtp_from_name' => 'required',
|
2023-08-31 11:10:39 +00:00
|
|
|
'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',
|
|
|
|
]);
|
|
|
|
$this->settings->save();
|
2024-02-22 13:53:42 +00:00
|
|
|
$this->dispatch('success', 'Settings saved.');
|
2023-09-11 15:36:30 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-09-15 13:34:25 +00:00
|
|
|
return handleError($e, $this);
|
2023-06-20 19:18:14 +00:00
|
|
|
}
|
2023-06-06 13:30:33 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
|
|
|
public function sendTestNotification()
|
|
|
|
{
|
2023-12-13 11:01:27 +00:00
|
|
|
$this->settings?->notify(new Test($this->emails));
|
2023-12-07 18:06:32 +00:00
|
|
|
$this->dispatch('success', 'Test email sent.');
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|
|
|
|
}
|