2023-06-06 13:30:33 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Livewire\Settings;
|
|
|
|
|
|
|
|
use App\Models\InstanceSettings;
|
2023-06-12 10:00:01 +00:00
|
|
|
use App\Notifications\TransactionalEmails\TestEmail;
|
2023-06-06 15:50:13 +00:00
|
|
|
use Illuminate\Support\Facades\Notification;
|
2023-06-06 13:30:33 +00:00
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
class Email extends Component
|
|
|
|
{
|
|
|
|
public InstanceSettings $settings;
|
|
|
|
|
|
|
|
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-06-06 13:30:33 +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-06-13 08:51:58 +00:00
|
|
|
];
|
2023-06-20 19:18:14 +00:00
|
|
|
public function mount()
|
|
|
|
{
|
|
|
|
$this->decrypt();
|
|
|
|
}
|
2023-06-13 08:51:58 +00:00
|
|
|
public function instantSave()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$this->submit();
|
2023-06-22 08:04:39 +00:00
|
|
|
$this->emit('success', 'Settings saved successfully.');
|
2023-06-13 08:51:58 +00:00
|
|
|
} catch (\Exception $e) {
|
2023-07-27 19:26:15 +00:00
|
|
|
$this->settings->smtp_enabled = false;
|
2023-06-13 08:51:58 +00:00
|
|
|
$this->validate();
|
|
|
|
}
|
|
|
|
}
|
2023-06-21 08:53:24 +00:00
|
|
|
public function testNotification()
|
2023-06-06 13:30:33 +00:00
|
|
|
{
|
2023-06-21 08:53:24 +00:00
|
|
|
$this->settings->notify(new TestEmail);
|
2023-06-19 08:58:00 +00:00
|
|
|
$this->emit('success', 'Test email sent.');
|
2023-06-06 13:30:33 +00:00
|
|
|
}
|
2023-06-20 19:18:14 +00:00
|
|
|
private function decrypt()
|
|
|
|
{
|
2023-07-27 19:26:15 +00:00
|
|
|
if (data_get($this->settings, 'smtp_password')) {
|
2023-06-20 19:18:14 +00:00
|
|
|
try {
|
2023-07-27 19:26:15 +00:00
|
|
|
$this->settings->smtp_password = decrypt($this->settings->smtp_password);
|
2023-06-20 19:18:14 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-06 13:30:33 +00:00
|
|
|
public function submit()
|
|
|
|
{
|
2023-06-20 19:18:14 +00:00
|
|
|
$this->resetErrorBag();
|
2023-06-06 13:30:33 +00:00
|
|
|
$this->validate();
|
2023-07-27 19:26:15 +00:00
|
|
|
if ($this->settings->smtp_password) {
|
|
|
|
$this->settings->smtp_password = encrypt($this->settings->smtp_password);
|
2023-06-20 19:18:14 +00:00
|
|
|
} else {
|
2023-07-27 19:26:15 +00:00
|
|
|
$this->settings->smtp_password = null;
|
2023-06-20 19:18:14 +00:00
|
|
|
}
|
|
|
|
|
2023-06-06 13:30:33 +00:00
|
|
|
$this->settings->save();
|
2023-06-22 08:04:39 +00:00
|
|
|
$this->emit('success', 'Transaction email settings updated successfully.');
|
2023-06-20 19:18:14 +00:00
|
|
|
$this->decrypt();
|
2023-06-06 13:30:33 +00:00
|
|
|
}
|
2023-07-27 19:26:15 +00:00
|
|
|
}
|