lasthourcloud/app/Http/Livewire/Settings/Email.php

81 lines
2.6 KiB
PHP
Raw Normal View History

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-06-20 17:08:43 +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.test_recipients' => '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-06-20 17:08:43 +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',
'settings.smtp.test_recipients' => 'Test Recipients',
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-06-20 17:08:43 +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()
{
if (data_get($this->settings, 'smtp.password')) {
try {
$this->settings->smtp->password = decrypt($this->settings->smtp->password);
} 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-06-20 19:18:14 +00:00
if ($this->settings->smtp->password) {
$this->settings->smtp->password = encrypt($this->settings->smtp->password);
} else {
$this->settings->smtp->password = null;
}
2023-06-20 17:08:43 +00:00
$this->settings->smtp->test_recipients = str_replace(' ', '', $this->settings->smtp->test_recipients);
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
}
}