187 lines
6.7 KiB
PHP
187 lines
6.7 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
|
||
/**
|
||
* Notification e-mail pour un nouveau message dans la messagerie interne.
|
||
*
|
||
* À inclure APRÈS $pdo->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 = '<!DOCTYPE html>
|
||
<html lang="fr">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||
<title>USLUGI.FR</title>
|
||
</head>
|
||
<body style="margin:0;padding:0;background:#f4f6f8;font-family:Arial,Helvetica,sans-serif;">
|
||
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="background:#f4f6f8;padding:24px 0;">
|
||
<tr>
|
||
<td align="center">
|
||
<table role="presentation" width="600" cellpadding="0" cellspacing="0" style="background:#ffffff;border-radius:8px;overflow:hidden;">
|
||
<tr>
|
||
<td style="background:#38378B;color:#ffffff;padding:20px 24px;font-size:18px;font-weight:600;">
|
||
<img
|
||
src="https://uslugi.fr/public/uploads/img/logo_mail.png"
|
||
alt="USLUGI.FR"
|
||
width="150">
|
||
</td>
|
||
</tr>
|
||
<tr>
|
||
<td style="padding:24px 24px 8px 24px;color:#111111;font-size:16px;line-height:1.5;">
|
||
' . $safeHello . '
|
||
</td>
|
||
</tr>
|
||
<tr>
|
||
<td style="padding:8px 24px 16px 24px;color:#111111;font-size:15px;line-height:1.6;">
|
||
' . $safeIntro . '
|
||
</td>
|
||
</tr>
|
||
<tr>
|
||
<td align="center" style="padding:8px 24px 24px 24px;">
|
||
<a href="' . $safeCtaUrl . '" target="_blank"
|
||
style="display:inline-block;text-decoration:none;padding:12px 22px;border-radius:999px;background:#E03C3F;color:#ffffff;font-weight:600;">
|
||
' . $safeCtaLbl . '
|
||
</a>
|
||
</td>
|
||
</tr>
|
||
<tr>
|
||
<td style="padding:0 24px 24px 24px;color:#111111;font-size:14px;line-height:1.6;">
|
||
' . $safeFooter . '
|
||
</td>
|
||
</tr>
|
||
</table>
|
||
<table role="presentation" width="600" cellpadding="0" cellspacing="0" style="margin-top:12px;">
|
||
<tr>
|
||
<td style="text-align:center;color:#9ca3af;font-size:12px;padding:8px 0;">
|
||
© ' . date('Y') . ' USLUGI.FR
|
||
</td>
|
||
</tr>
|
||
</table>
|
||
</td>
|
||
</tr>
|
||
</table>
|
||
</body>
|
||
</html>';
|
||
|
||
// ===== 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());
|
||
}
|
||
}
|