From 9ca794b1bc1ed8b4717dd6751261764a87ba7911 Mon Sep 17 00:00:00 2001 From: Claudio Sironi Date: Fri, 30 Oct 2026 11:52:07 +0100 Subject: [PATCH] fixed sending email --- public/conferma_pending.php | 100 +++++++++++++++++++++++++++++-- public/removebooking.php | 113 +++++++++++++++++++++++++++++------- 2 files changed, 188 insertions(+), 25 deletions(-) diff --git a/public/conferma_pending.php b/public/conferma_pending.php index 95cc29e..9c05255 100644 --- a/public/conferma_pending.php +++ b/public/conferma_pending.php @@ -2,9 +2,16 @@ // Buffer per scartare eventuale HTML degli include: risposta JSON pulita ob_start(); require_once('include/headscript.php'); +require_once __DIR__ . '/class/mailer.php'; + +use PHPMailer\PHPMailer\PHPMailer; /** * Conferma una prenotazione in attesa: pending -> booked. + * Se è una riprogrammazione (prevbookingstart valorizzato) imposta anche + * is_reprogrammed = 'Y', come acceptbookingclass.php. + * Dopo la conferma avvisa l'allieva via email (se l'invio fallisce la + * conferma resta valida e l'errore va nel log). */ $pdo = DBHandlerSelect::getInstance()->getConnection(); @@ -25,14 +32,99 @@ if ($idbookingclass <= 0) { } try { - // Conferma solo se effettivamente in pending (evita conferme accidentali) + // Dati della prenotazione e dell'allieva $stmt = $pdo->prepare( - "UPDATE bookingclass SET status = 'booked' - WHERE idbookingclass = :id AND status = 'pending'" + "SELECT bc.status, bc.prevbookingstart, bc.bookingstart, + u.first_name, u.email, s.servicename + FROM bookingclass bc + LEFT JOIN auth_users u ON u.id = bc.iduser + LEFT JOIN service s ON s.idservice = bc.idservice + WHERE bc.idbookingclass = :id" ); $stmt->execute([':id' => $idbookingclass]); - jsonOut(['success' => true]); + $bk = $stmt->fetch(PDO::FETCH_ASSOC); + + if (!$bk || $bk['status'] !== 'pending') { + jsonOut(['success' => false, 'error' => 'La prenotazione non è più in attesa.']); + } + + $isReprogram = !empty($bk['prevbookingstart']); + + // Conferma solo se effettivamente in pending (evita conferme accidentali) + $sql = "UPDATE bookingclass SET status = 'booked'" + . ($isReprogram ? ", is_reprogrammed = 'Y'" : "") + . " WHERE idbookingclass = :id AND status = 'pending'"; + $stmt = $pdo->prepare($sql); + $stmt->execute([':id' => $idbookingclass]); + + if ($stmt->rowCount() !== 1) { + jsonOut(['success' => false, 'error' => 'La prenotazione non è più in attesa.']); + } } catch (Throwable $ex) { error_log('conferma_pending error: ' . $ex->getMessage()); jsonOut(['success' => false, 'error' => 'Errore durante la conferma.']); } + +// ---- Email all'allieva (dopo l'aggiornamento) ---- +$mailSent = false; + +if (!empty($bk['email'])) { + $nome = htmlspecialchars((string) $bk['first_name'], ENT_QUOTES, 'UTF-8'); + $classe = htmlspecialchars((string) ($bk['servicename'] ?? ''), ENT_QUOTES, 'UTF-8'); + $quando = date('d-m-Y H:i', strtotime($bk['bookingstart'])); + + if ($isReprogram) { + $subject = 'YogiBook - Lezione riprogrammata con successo!'; + $intro = 'La tua richiesta di riprogrammazione è stata accettata!'; + } else { + $subject = 'YogiBook - Prenotazione confermata!'; + $intro = 'La tua prenotazione è stata confermata!'; + } + + $p = "

"; + $pc = "

"; + + // Variabili usate da mail/emailtemplate2.php + $messageedit = "

Ciao $nome ,

" + . $p . $intro . $pc + . $p . "Lezione: $classe del $quando" . $pc + . "
" + . $p . "Per vedere e gestire le tue lezioni clicca qui: https://yogibook.yogasoul.it " . $pc + . "
" + . $p . "Ci vediamo sul tappetino!" . $pc + . $p . "Il Team Yogasoul" . $pc; + + $buttonedit = " + YogiBook - YogaSoul + "; + + try { + include('mail/emailtemplate2.php'); // crea $mailmessage1 + + $mail = new PHPMailer(true); + $mail->isSMTP(); + $mail->Host = $_ENV['MAIL_HOST']; + $mail->SMTPAuth = true; + $mail->Username = $_ENV['MAIL_USERNAME']; + $mail->Password = $_ENV['MAIL_PASSWORD']; + $mail->SMTPSecure = $_ENV['MAIL_ENCRYPTION']; + $mail->Port = (int) $_ENV['MAIL_PORT']; + $mail->CharSet = 'UTF-8'; + + $mail->From = $_ENV['MAIL_FROM_ADDRESS']; + $mail->FromName = $_ENV['MAIL_FROM_NAME']; + $mail->addAddress($bk['email']); + + $mail->isHTML(true); + $mail->Subject = $subject; + $mail->Body = $mailmessage1; + $mail->AltBody = $intro . ' Lezione: ' . ($bk['servicename'] ?? '') . ' del ' . $quando; + + $mail->send(); + $mailSent = true; + } catch (Throwable $ex) { + error_log('conferma_pending mail (booking ' . $idbookingclass . '): ' . $ex->getMessage()); + } +} + +jsonOut(['success' => true, 'mail' => $mailSent]); diff --git a/public/removebooking.php b/public/removebooking.php index c564ca2..797c87d 100644 --- a/public/removebooking.php +++ b/public/removebooking.php @@ -1,33 +1,104 @@ connect_error) { - die("Connessione al database fallita: " . $conn->connect_error); -} +use PHPMailer\PHPMailer\PHPMailer; -// Verifica se è stato passato il parametro GET "idbookingclass" -if (isset($_GET['idbookingclass'])) { - $idbookingclass = $_GET['idbookingclass']; +/** + * Pannello Classi: cestino. Elimina una prenotazione (confermata, in attesa o in coda). + * Se era una richiesta in attesa (pending), cioè un rifiuto, avvisa l'allieva + * via email come rejectbookingclass.php. Per confermate e coda: nessuna email. + */ +$pdo = DBHandlerSelect::getInstance()->getConnection(); - // Query per rimuovere il record dalla tabella "bookingclass" - $sql = "DELETE FROM bookingclass WHERE idbookingclass = $idbookingclass"; +$idbookingclass = isset($_GET['idbookingclass']) ? (int) $_GET['idbookingclass'] : 0; - if ($conn->query($sql) === TRUE) { - echo "Record rimosso con successo."; - } else { - echo "Errore nella rimozione del record: " . $conn->error; +$bk = null; +$deleted = false; + +if ($idbookingclass > 0) { + try { + // Dati letti prima di eliminare (servono per l'email) + $stmt = $pdo->prepare( + "SELECT bc.status, bc.prevbookingstart, bc.bookingstart, + u.first_name, u.email, s.servicename + FROM bookingclass bc + LEFT JOIN auth_users u ON u.id = bc.iduser + LEFT JOIN service s ON s.idservice = bc.idservice + WHERE bc.idbookingclass = :id" + ); + $stmt->execute([':id' => $idbookingclass]); + $bk = $stmt->fetch(PDO::FETCH_ASSOC); + + if ($bk) { + $stmt = $pdo->prepare("DELETE FROM bookingclass WHERE idbookingclass = :id"); + $stmt->execute([':id' => $idbookingclass]); + $deleted = ($stmt->rowCount() === 1); + } + } catch (Throwable $ex) { + error_log('removebooking error (booking ' . $idbookingclass . '): ' . $ex->getMessage()); } } -// Chiusura della connessione al database -$conn->close(); +// ---- Email solo se è stata rifiutata una richiesta in attesa ---- +if ($deleted && $bk['status'] === 'pending' && !empty($bk['email'])) { + $nome = htmlspecialchars((string) $bk['first_name'], ENT_QUOTES, 'UTF-8'); + $classe = htmlspecialchars((string) ($bk['servicename'] ?? ''), ENT_QUOTES, 'UTF-8'); + $quando = date('d-m-Y H:i', strtotime($bk['bookingstart'])); + + if (!empty($bk['prevbookingstart'])) { + $subject = 'YogiBook - Riprogrammazione rifiutata!'; + $intro = 'Ci dispiace ma la tua richiesta di riprogrammazione è stata rifiutata!'; + } else { + $subject = 'YogiBook - Prenotazione rifiutata!'; + $intro = 'Ci dispiace ma la tua richiesta di prenotazione è stata rifiutata!'; + } + + $p = "

"; + $pc = "

"; + + // Variabili usate da mail/emailtemplate2.php + $messageedit = "

Ciao $nome ,

" + . $p . $intro . $pc + . $p . "Lezione richiesta: $classe del $quando" . $pc + . $p . "Puoi collegarti al portale YogiBook come sotto riportato per programmare la tua lezione" . $pc + . "
" + . $p . "Per vedere e gestire le tue lezioni clicca qui: https://yogibook.yogasoul.it " . $pc + . "
" + . $p . "Ci vediamo sul tappetino!" . $pc + . $p . "Il Team Yogasoul" . $pc; + + $buttonedit = " + YogiBook - YogaSoul + "; + + try { + include('mail/emailtemplate2.php'); // crea $mailmessage1 + + $mail = new PHPMailer(true); + $mail->isSMTP(); + $mail->Host = $_ENV['MAIL_HOST']; + $mail->SMTPAuth = true; + $mail->Username = $_ENV['MAIL_USERNAME']; + $mail->Password = $_ENV['MAIL_PASSWORD']; + $mail->SMTPSecure = $_ENV['MAIL_ENCRYPTION']; + $mail->Port = (int) $_ENV['MAIL_PORT']; + $mail->CharSet = 'UTF-8'; + + $mail->From = $_ENV['MAIL_FROM_ADDRESS']; + $mail->FromName = $_ENV['MAIL_FROM_NAME']; + $mail->addAddress($bk['email']); + + $mail->isHTML(true); + $mail->Subject = $subject; + $mail->Body = $mailmessage1; + $mail->AltBody = $intro . ' Lezione richiesta: ' . ($bk['servicename'] ?? '') . ' del ' . $quando; + + $mail->send(); + } catch (Throwable $ex) { + error_log('removebooking mail (booking ' . $idbookingclass . '): ' . $ex->getMessage()); + } +} -// Reindirizzamento alla pagina adminpanel.php header("Location: adminpanel.php"); exit; -?> - -