commit() dans le script d'envoi de message :
*
* try {
* require_once __DIR__ . '/../public/message_par_mail.php';
* notifier_nouveau_message_par_email($pdo, $convId, $role);
* } catch (Throwable $e) {
* error_log('Notif mail error: '.$e->getMessage());
* }
*
* - $pdo : PDO connecté à la base uslugi
* - $convId : ID de la conversation
* - $role : 'praticien' ou 'client' (l'expéditeur)
*/
require_once __DIR__ . '/../../includes/bootstrap_global.php';
// On suppose que $translations est déjà chargé par _bootstrap/lang.php.
// Si ce n’est pas le cas, on évite de crasher.
if (!isset($translations) || !is_array($translations)) {
$translations = [];
}
/**
* Envoie un e-mail de notification pour un nouveau message.
*
* @param PDO $pdo
* @param int $conversationId ID de la conversation
* @param string $senderRole 'praticien' ou 'client'
*/
function notifier_nouveau_message_par_email(PDO $pdo, int $conversationId, string $senderRole): void
{
global $translations;
if ($senderRole !== 'praticien' && $senderRole !== 'client') {
return;
}
// Récupérer la conversation + emails praticien/client
$sql = "
SELECT
c.id AS conv_id,
c.praticien_id AS prat_id,
c.client_id AS cli_id,
p.email AS prat_email,
p.nom AS prat_nom,
p.prenom AS prat_prenom,
cl.email AS cli_email,
cl.nom AS cli_nom,
cl.prenom AS cli_prenom
FROM conversations c
INNER JOIN praticien p ON p.id = c.praticien_id
INNER JOIN client cl ON cl.id = c.client_id
WHERE c.id = ?
LIMIT 1
";
$stmt = $pdo->prepare($sql);
$stmt->execute([$conversationId]);
$row = $stmt->fetch(\PDO::FETCH_ASSOC);
if (!$row) {
return; // conversation introuvable
}
// Déterminer le destinataire
if ($senderRole === 'praticien') {
// Le praticien écrit → on notifie le client
$toEmail = (string)($row['cli_email'] ?? '');
$toName = trim(((string)($row['cli_prenom'] ?? '')) . ' ' . ((string)($row['cli_nom'] ?? '')));
} else {
// Le client écrit → on notifie le praticien
$toEmail = (string)($row['prat_email'] ?? '');
$toName = trim(((string)($row['prat_prenom'] ?? '')) . ' ' . ((string)($row['prat_nom'] ?? '')));
}
if (!filter_var($toEmail, FILTER_VALIDATE_EMAIL)) {
return;
}
// ===== Textes via $translations (avec fallback FR) =====
$subject = $translations['email_new_msg_subject']
?? 'Vous avez un nouveau message sur USLUGI.FR';
$helloTmpl = $translations['email_new_msg_hello']
?? 'Bonjour %s,';
$helloLine = sprintf($helloTmpl, $toName !== '' ? $toName : '');
$intro = $translations['email_new_msg_intro']
?? 'Vous avez reçu un nouveau message dans votre messagerie USLUGI.FR.';
$ctaLabel = $translations['email_new_msg_cta_label']
?? 'Se connecter à USLUGI.FR';
$footer = $translations['email_new_msg_footer']
?? 'Merci d\'utiliser USLUGI.FR.';
// Lien générique de connexion (sans rôle, ni token)
$ctaUrl = 'https://uslugi.fr/connexion.php';
// ===== Version texte =====
$textLines = [];
$textLines[] = $helloLine;
$textLines[] = '';
$textLines[] = $intro;
$textLines[] = '';
$textLines[] = $ctaLabel . ' : ' . $ctaUrl;
$textLines[] = '';
$textLines[] = $footer;
$textBody = implode("\n", $textLines);
// ===== Version HTML =====
$safeHello = htmlspecialchars($helloLine, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
$safeIntro = nl2br(htmlspecialchars($intro, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'));
$safeFooter = nl2br(htmlspecialchars($footer, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'));
$safeCtaLbl = htmlspecialchars($ctaLabel, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
$safeCtaUrl = htmlspecialchars($ctaUrl, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
$htmlBody = '
USLUGI.FR
|
© ' . date('Y') . ' USLUGI.FR
|
|
';
// ===== Envoi via helper uslugi_mail() =====
try {
[$ok, $err] = uslugi_mail($toEmail, $subject, $htmlBody, $textBody);
if (!$ok && $err) {
error_log('uslugi_mail(new_message) error: ' . $err);
}
} catch (\Throwable $e) {
error_log('Exception uslugi_mail(new_message): ' . $e->getMessage());
}
}