Files
xamxam/app/public/admin/actions/smtp-test.php

65 lines
2.0 KiB
PHP

<?php
require_once __DIR__ . "/../../../bootstrap.php";
require_once __DIR__ . '/../../../src/AdminAuth.php';
AdminAuth::requireLogin();
require_once APP_ROOT . '/src/Database.php';
require_once APP_ROOT . '/src/SmtpRelay.php';
require_once APP_ROOT . '/src/App.php';
// CSRF
if (
!isset($_POST['csrf_token'], $_SESSION['csrf_token']) ||
!hash_equals($_SESSION['csrf_token'], $_POST['csrf_token'])
) {
App::flash('error', 'Erreur de sécurité : token invalide.');
header('Location: /admin/parametres.php');
exit;
}
$to = trim($_POST['test_email'] ?? '');
if ($to === '' || !filter_var($to, FILTER_VALIDATE_EMAIL)) {
App::flash('error', 'Adresse e-mail de test invalide.');
header('Location: /admin/parametres.php');
exit;
}
$db = new Database();
if (!SmtpRelay::isConfigured($db)) {
App::flash('error', 'Le relay SMTP n\'est pas configuré.');
header('Location: /admin/parametres.php');
exit;
}
$subject = 'Test SMTP — Post-ERG';
$sentDate = date('d/m/Y à H:i:s');
$toSafe = htmlspecialchars($to);
$body = <<<HTML
<html>
<body style="font-family: sans-serif; color: #222; max-width: 600px; margin: 0 auto; padding: 24px;">
<h1 style="font-size: 1.4rem; border-bottom: 2px solid #c00; padding-bottom: 8px;">
Test d'envoi SMTP — Post-ERG
</h1>
<p>Ceci est un e-mail de test envoyé depuis l'interface d'administration de <strong>Post-ERG</strong>.</p>
<p>Si vous recevez ce message, la configuration du relay SMTP est correcte.</p>
<hr style="border:none; border-top:1px solid #ddd; margin: 24px 0;">
<p style="font-size: 0.85rem; color: #666;">
Envoyé le : $sentDate<br>
Destinataire : $toSafe
</p>
</body>
</html>
HTML;
$ok = SmtpRelay::send($db, $to, $subject, $body);
if ($ok) {
App::flash('success', "E-mail de test envoyé à \u00ab\u00a0{$to}\u00a0\u00bb.");
} else {
App::flash('error', "Échec de l'envoi. Vérifiez la configuration SMTP et les logs serveur.");
}
header('Location: /admin/parametres.php');
exit;