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

81 lines
2.6 KiB
PHP
Raw Normal View History

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