add SMTP sender
This commit is contained in:
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\EmailSetting;
|
||||
use App\Models\SenderAccount;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Crypt;
|
||||
|
||||
@@ -13,7 +14,8 @@ class EmailSettingsController extends Controller
|
||||
{
|
||||
$this->authorizeWrite('settings');
|
||||
$settings = EmailSetting::first() ?? new EmailSetting();
|
||||
return view('admin.email-settings.index', compact('settings'));
|
||||
$senderAccounts = SenderAccount::orderBy('email_address')->get();
|
||||
return view('admin.email-settings.index', compact('settings', 'senderAccounts'));
|
||||
}
|
||||
|
||||
public function save(Request $request)
|
||||
@@ -183,4 +185,91 @@ class EmailSettingsController extends Controller
|
||||
return back()->with('error', 'Errore sync: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function senderStore(Request $request)
|
||||
{
|
||||
$this->authorizeWrite('settings');
|
||||
$data = $request->validate([
|
||||
'email_address' => 'required|email|max:255|unique:sender_accounts,email_address',
|
||||
'email_name' => 'nullable|string|max:255',
|
||||
'smtp_host' => 'nullable|string|max:255',
|
||||
'smtp_port' => 'nullable|integer|min:1|max:65535',
|
||||
'smtp_encryption' => 'nullable|in:tls,ssl,none',
|
||||
'smtp_username' => 'nullable|string|max:255',
|
||||
'smtp_password' => 'nullable|string',
|
||||
'reply_to' => 'nullable|email|max:255',
|
||||
'verify_email' => 'nullable|email|max:255',
|
||||
'note' => 'nullable|string|max:500',
|
||||
'is_active' => 'boolean',
|
||||
]);
|
||||
|
||||
if (!empty($data['smtp_password'])) {
|
||||
$data['smtp_password'] = Crypt::encryptString($data['smtp_password']);
|
||||
} else {
|
||||
unset($data['smtp_password']);
|
||||
}
|
||||
|
||||
$sender = SenderAccount::create($data);
|
||||
|
||||
return back()->with('success', 'Mittente "' . $sender->email_address . '" creato con successo.');
|
||||
}
|
||||
|
||||
public function senderUpdate(Request $request, int $id)
|
||||
{
|
||||
$this->authorizeWrite('settings');
|
||||
$sender = SenderAccount::findOrFail($id);
|
||||
|
||||
$data = $request->validate([
|
||||
'email_address' => 'required|email|max:255|unique:sender_accounts,email_address,' . $id,
|
||||
'email_name' => 'nullable|string|max:255',
|
||||
'smtp_host' => 'nullable|string|max:255',
|
||||
'smtp_port' => 'nullable|integer|min:1|max:65535',
|
||||
'smtp_encryption' => 'nullable|in:tls,ssl,none',
|
||||
'smtp_username' => 'nullable|string|max:255',
|
||||
'smtp_password' => 'nullable|string',
|
||||
'reply_to' => 'nullable|email|max:255',
|
||||
'verify_email' => 'nullable|email|max:255',
|
||||
'note' => 'nullable|string|max:500',
|
||||
'is_active' => 'boolean',
|
||||
]);
|
||||
|
||||
if (!empty($data['smtp_password'])) {
|
||||
$data['smtp_password'] = Crypt::encryptString($data['smtp_password']);
|
||||
} else {
|
||||
unset($data['smtp_password']);
|
||||
}
|
||||
|
||||
$sender->update($data);
|
||||
|
||||
return back()->with('success', 'Mittente "' . $sender->email_address . '" aggiornato.');
|
||||
}
|
||||
|
||||
public function senderDestroy(int $id)
|
||||
{
|
||||
$this->authorizeDelete('settings');
|
||||
$sender = SenderAccount::findOrFail($id);
|
||||
$sender->delete();
|
||||
|
||||
return back()->with('success', 'Mittente eliminato.');
|
||||
}
|
||||
|
||||
public function senderTestSmtp(Request $request, int $id)
|
||||
{
|
||||
$this->authorizeWrite('settings');
|
||||
$request->validate(['email' => 'required|email']);
|
||||
|
||||
$sender = SenderAccount::findOrFail($id);
|
||||
|
||||
try {
|
||||
$sender->sendEmail(
|
||||
$request->email,
|
||||
'Test Configurazione SMTP - ' . config('app.name'),
|
||||
'Test email da ' . config('app.name') . ' - Configurazione SMTP mittente "' . $sender->email_address . '" funziona!'
|
||||
);
|
||||
|
||||
return response()->json(['success' => true, 'message' => 'Email di test inviata a ' . $request->email]);
|
||||
} catch (\Exception $e) {
|
||||
return response()->json(['success' => false, 'message' => 'Errore invio: ' . $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user