email support
This commit is contained in:
@@ -10,6 +10,10 @@
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
use Symfony\Component\Mailer\Mailer as SymfonyMailer;
|
||||
use Symfony\Component\Mailer\Transport;
|
||||
use Symfony\Component\Mime\Address;
|
||||
use Symfony\Component\Mime\Email;
|
||||
|
||||
class MailProfileController extends Controller
|
||||
{
|
||||
@@ -92,4 +96,65 @@ public function destroy(MailProfile $mailProfile)
|
||||
|
||||
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 = '<p>This is a <strong>test email</strong> from profile <code>'.e($mailProfile->name).'</code> at '.e(now()->toDateTimeString()).'.</p>';
|
||||
$text = 'This is a test email from profile "'.$mailProfile->name.'" at '.now()->toDateTimeString().'.';
|
||||
|
||||
// Build email
|
||||
$email = (new Email)
|
||||
->from(new Address($fromAddr, $fromName))
|
||||
->to($to)
|
||||
->subject('Test email - '.$mailProfile->name)
|
||||
->text($text)
|
||||
->html($html);
|
||||
|
||||
$mailer->send($email);
|
||||
|
||||
$mailProfile->forceFill([
|
||||
'last_success_at' => now(),
|
||||
'last_error_at' => null,
|
||||
'last_error_message' => null,
|
||||
])->save();
|
||||
|
||||
return back()->with('success', 'Test email sent to '.$to);
|
||||
} catch (\Throwable $e) {
|
||||
$mailProfile->forceFill([
|
||||
'last_error_at' => now(),
|
||||
'last_error_message' => $e->getMessage(),
|
||||
])->save();
|
||||
|
||||
return back()->with('error', 'Failed to send test: '.$e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user