authorize('viewAny', MailProfile::class); $profiles = MailProfile::query() ->orderBy('priority') ->orderBy('id') ->get([ 'id', 'name', 'active', 'host', 'port', 'encryption', 'from_address', 'priority', 'last_success_at', 'last_error_at', 'last_error_message', 'test_status', 'test_checked_at', ]); return Inertia::render('Admin/MailProfiles/Index', [ 'profiles' => $profiles, ]); } public function store(StoreMailProfileRequest $request) { $data = $request->validated(); $profile = new MailProfile; foreach ($data as $key => $val) { if ($key === 'password') { $profile->password = $val; // triggers mutator to encrypt } else { $profile->{$key} = $val; } } $profile->save(); return back()->with('success', 'Mail profile created'); } public function update(UpdateMailProfileRequest $request, MailProfile $mailProfile) { $data = $request->validated(); foreach ($data as $key => $val) { if ($key === 'password') { if ($val !== null && $val !== '') { $mailProfile->password = $val; } } else { $mailProfile->{$key} = $val; } } $mailProfile->save(); return back()->with('success', 'Mail profile updated'); } public function toggle(Request $request, MailProfile $mailProfile) { $this->authorize('update', $mailProfile); $mailProfile->active = ! $mailProfile->active; $mailProfile->save(); return back()->with('success', 'Status updated'); } public function test(Request $request, MailProfile $mailProfile) { $this->authorize('test', $mailProfile); $mailProfile->forceFill([ 'test_status' => 'queued', 'test_checked_at' => now(), ])->save(); \App\Jobs\TestMailProfileConnection::dispatch($mailProfile->id); return back()->with('success', 'Test queued'); } public function destroy(MailProfile $mailProfile) { $this->authorize('delete', $mailProfile); $mailProfile->delete(); return back()->with('success', 'Mail profile deleted'); } public function sendTest(Request $request, MailProfile $mailProfile) { $this->authorize('test', $mailProfile); $to = (string) ($request->input('to') ?: $mailProfile->from_address); if ($to === '' || ! filter_var($to, FILTER_VALIDATE_EMAIL)) { return back()->with('error', 'Missing or invalid target email address'); } // Build DSN for Symfony Mailer transport based on profile $host = $mailProfile->host; $port = (int) ($mailProfile->port ?: 587); $encryption = $mailProfile->encryption ?: 'tls'; $username = $mailProfile->username ?: ''; $password = (string) ($mailProfile->decryptPassword() ?? ''); // Map encryption to Symfony DSN $scheme = $encryption === 'ssl' ? 'smtps' : 'smtp'; $query = ''; if ($encryption === 'tls') { $query = '?encryption=tls'; } $dsn = sprintf('%s://%s:%s@%s:%d%s', $scheme, rawurlencode($username), rawurlencode($password), $host, $port, $query); try { $transport = Transport::fromDsn($dsn); $mailer = new SymfonyMailer($transport); $fromAddr = $mailProfile->from_address ?: $username; $fromName = $mailProfile->from_name ?: config('app.name'); $html = '
This is a test email from profile '.e($mailProfile->name).' at '.e(now()->toDateTimeString()).'.