fixed sending email
This commit is contained in:
@@ -2,9 +2,16 @@
|
|||||||
// Buffer per scartare eventuale HTML degli include: risposta JSON pulita
|
// Buffer per scartare eventuale HTML degli include: risposta JSON pulita
|
||||||
ob_start();
|
ob_start();
|
||||||
require_once('include/headscript.php');
|
require_once('include/headscript.php');
|
||||||
|
require_once __DIR__ . '/class/mailer.php';
|
||||||
|
|
||||||
|
use PHPMailer\PHPMailer\PHPMailer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Conferma una prenotazione in attesa: pending -> booked.
|
* 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();
|
$pdo = DBHandlerSelect::getInstance()->getConnection();
|
||||||
|
|
||||||
@@ -25,14 +32,99 @@ if ($idbookingclass <= 0) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Conferma solo se effettivamente in pending (evita conferme accidentali)
|
// Dati della prenotazione e dell'allieva
|
||||||
$stmt = $pdo->prepare(
|
$stmt = $pdo->prepare(
|
||||||
"UPDATE bookingclass SET status = 'booked'
|
"SELECT bc.status, bc.prevbookingstart, bc.bookingstart,
|
||||||
WHERE idbookingclass = :id AND status = 'pending'"
|
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]);
|
$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) {
|
} catch (Throwable $ex) {
|
||||||
error_log('conferma_pending error: ' . $ex->getMessage());
|
error_log('conferma_pending error: ' . $ex->getMessage());
|
||||||
jsonOut(['success' => false, 'error' => 'Errore durante la conferma.']);
|
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 = "<p style='font-size: 14px; line-height: 190%;'><span style='font-size: 16px; line-height: 30.4px;'>";
|
||||||
|
$pc = "</span></p>";
|
||||||
|
|
||||||
|
// Variabili usate da mail/emailtemplate2.php
|
||||||
|
$messageedit = "<p style='font-size: 14px; line-height: 190%;'><span style='font-size: 18px; line-height: 34.2px;'><strong><span style='line-height: 34.2px; font-size: 18px;'> Ciao $nome , </span></strong></span></p>"
|
||||||
|
. $p . $intro . $pc
|
||||||
|
. $p . "Lezione: $classe del $quando" . $pc
|
||||||
|
. "<br>"
|
||||||
|
. $p . "Per vedere e gestire le tue lezioni clicca qui: https://yogibook.yogasoul.it " . $pc
|
||||||
|
. "<br>"
|
||||||
|
. $p . "Ci vediamo sul tappetino!" . $pc
|
||||||
|
. $p . "Il Team Yogasoul" . $pc;
|
||||||
|
|
||||||
|
$buttonedit = "<a href='https://yogibook.yogasoul.it/' target='_blank' class='v-button v-font-size' style='box-sizing: border-box;display: inline-block;text-decoration: none;-webkit-text-size-adjust: none;text-align: center;color: #FFFFFF; background-color: #3AAEE0; border-radius: 4px;-webkit-border-radius: 4px; -moz-border-radius: 4px; width:auto; max-width:100%; overflow-wrap: break-word; word-break: break-word; word-wrap:break-word; mso-border-alt: none;font-size: 14px;'>
|
||||||
|
<span style='display:block;padding:10px 20px;line-height:120%;'><span style='line-height: 16.8px;'>YogiBook - YogaSoul</span></span>
|
||||||
|
</a>";
|
||||||
|
|
||||||
|
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]);
|
||||||
|
|||||||
+92
-21
@@ -1,33 +1,104 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
include('include/headscript.php');
|
include('include/headscript.php');
|
||||||
$conn = new mysqli($servername, $username, $password, $dbname);
|
require_once __DIR__ . '/class/mailer.php';
|
||||||
|
|
||||||
// Verifica della connessione
|
use PHPMailer\PHPMailer\PHPMailer;
|
||||||
if ($conn->connect_error) {
|
|
||||||
die("Connessione al database fallita: " . $conn->connect_error);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verifica se è stato passato il parametro GET "idbookingclass"
|
/**
|
||||||
if (isset($_GET['idbookingclass'])) {
|
* Pannello Classi: cestino. Elimina una prenotazione (confermata, in attesa o in coda).
|
||||||
$idbookingclass = $_GET['idbookingclass'];
|
* 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"
|
$idbookingclass = isset($_GET['idbookingclass']) ? (int) $_GET['idbookingclass'] : 0;
|
||||||
$sql = "DELETE FROM bookingclass WHERE idbookingclass = $idbookingclass";
|
|
||||||
|
|
||||||
if ($conn->query($sql) === TRUE) {
|
$bk = null;
|
||||||
echo "Record rimosso con successo.";
|
$deleted = false;
|
||||||
} else {
|
|
||||||
echo "Errore nella rimozione del record: " . $conn->error;
|
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
|
// ---- Email solo se è stata rifiutata una richiesta in attesa ----
|
||||||
$conn->close();
|
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 = "<p style='font-size: 14px; line-height: 190%;'><span style='font-size: 16px; line-height: 30.4px;'>";
|
||||||
|
$pc = "</span></p>";
|
||||||
|
|
||||||
|
// Variabili usate da mail/emailtemplate2.php
|
||||||
|
$messageedit = "<p style='font-size: 14px; line-height: 190%;'><span style='font-size: 18px; line-height: 34.2px;'><strong><span style='line-height: 34.2px; font-size: 18px;'> Ciao $nome , </span></strong></span></p>"
|
||||||
|
. $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
|
||||||
|
. "<br>"
|
||||||
|
. $p . "Per vedere e gestire le tue lezioni clicca qui: https://yogibook.yogasoul.it " . $pc
|
||||||
|
. "<br>"
|
||||||
|
. $p . "Ci vediamo sul tappetino!" . $pc
|
||||||
|
. $p . "Il Team Yogasoul" . $pc;
|
||||||
|
|
||||||
|
$buttonedit = "<a href='https://yogibook.yogasoul.it/' target='_blank' class='v-button v-font-size' style='box-sizing: border-box;display: inline-block;text-decoration: none;-webkit-text-size-adjust: none;text-align: center;color: #FFFFFF; background-color: #3AAEE0; border-radius: 4px;-webkit-border-radius: 4px; -moz-border-radius: 4px; width:auto; max-width:100%; overflow-wrap: break-word; word-break: break-word; word-wrap:break-word; mso-border-alt: none;font-size: 14px;'>
|
||||||
|
<span style='display:block;padding:10px 20px;line-height:120%;'><span style='line-height: 16.8px;'>YogiBook - YogaSoul</span></span>
|
||||||
|
</a>";
|
||||||
|
|
||||||
|
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");
|
header("Location: adminpanel.php");
|
||||||
exit;
|
exit;
|
||||||
?>
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user