added notification and cancellation mail
This commit is contained in:
@@ -0,0 +1,162 @@
|
||||
<?php
|
||||
// Abilita visualizzazione errori PHP
|
||||
ini_set('display_errors', 1);
|
||||
ini_set('display_startup_errors', 1);
|
||||
error_reporting(E_ALL);
|
||||
|
||||
use PHPMailer\PHPMailer\PHPMailer;
|
||||
use PHPMailer\PHPMailer\Exception;
|
||||
|
||||
include('include/headscript.php');
|
||||
|
||||
// Verifica connessione al database
|
||||
$conn = new mysqli($servername, $username, $password, $dbname);
|
||||
if ($conn->connect_error) {
|
||||
die("Connessione al database fallita: " . $conn->connect_error);
|
||||
}
|
||||
|
||||
// Inizializza contatore e log
|
||||
$emailCount = 0;
|
||||
$errors = [];
|
||||
$logFile = 'promemoria_cron_log.txt';
|
||||
$logMessage = "Esecuzione cron: " . date('Y-m-d H:i:s') . "\n";
|
||||
|
||||
// Seleziona prenotazioni per domani
|
||||
$tomorrow = date('Y-m-d', strtotime('+1 day'));
|
||||
$query = "SELECT bc.*, au.email, au.first_name, s.servicename
|
||||
FROM bookingclass bc
|
||||
LEFT JOIN auth_users au ON bc.iduser = au.id
|
||||
LEFT JOIN service s ON bc.idservice = s.idservice
|
||||
WHERE DATE(bc.bookingstart) = ? AND bc.status = 'booked'";
|
||||
|
||||
$stmt = $conn->prepare($query);
|
||||
if (!$stmt) {
|
||||
$errors[] = "Errore preparazione query: " . $conn->error;
|
||||
file_put_contents($logFile, $logMessage . "Errore query: " . $conn->error . "\n", FILE_APPEND);
|
||||
echo "Errore preparazione query: " . $conn->error;
|
||||
exit;
|
||||
}
|
||||
|
||||
$stmt->bind_param("s", $tomorrow);
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
|
||||
if ($result->num_rows === 0) {
|
||||
$logMessage .= "Nessuna prenotazione trovata per domani.\n";
|
||||
file_put_contents($logFile, $logMessage, FILE_APPEND);
|
||||
echo "Nessuna prenotazione trovata per domani.\n";
|
||||
exit;
|
||||
}
|
||||
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
$idbookingclass = $row['idbookingclass'];
|
||||
$token = $row['cancellation_token'];
|
||||
|
||||
// Genera token se assente
|
||||
if (empty($token)) {
|
||||
$token = bin2hex(random_bytes(32));
|
||||
$updateQuery = "UPDATE bookingclass SET cancellation_token = ? WHERE idbookingclass = ?";
|
||||
$updateStmt = $conn->prepare($updateQuery);
|
||||
if ($updateStmt) {
|
||||
$updateStmt->bind_param("si", $token, $idbookingclass);
|
||||
$updateStmt->execute();
|
||||
} else {
|
||||
$errors[] = "Errore preparazione query token per ID $idbookingclass: " . $conn->error;
|
||||
}
|
||||
}
|
||||
|
||||
$firstname = $row['first_name'] ?? 'Utente';
|
||||
$emailuser = $row['email'];
|
||||
$servicename = $row['servicename'] ?? 'Sconosciuta';
|
||||
$bookingstart = $row['bookingstart'];
|
||||
$dataformat = date("d-m-Y H:i", strtotime($bookingstart));
|
||||
|
||||
// Verifica email valida
|
||||
if (!filter_var($emailuser, FILTER_VALIDATE_EMAIL)) {
|
||||
$errors[] = "Email non valida per ID $idbookingclass: $emailuser";
|
||||
$logMessage .= "Email non valida per ID $idbookingclass: $emailuser\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
// Link cancellazione
|
||||
$link = "https://yogibook.yogasoul.it/cancella-prenotazione.php?idbookingclass=$idbookingclass&token=$token";
|
||||
|
||||
// Messaggio email
|
||||
$message = "<p style='font-size: 14px; line-height: 190%;'><span style='font-size: 18px; line-height: 34.2px;'><strong>Ciao $firstname,</strong></span></p>
|
||||
<p style='font-size: 14px; line-height: 190%;'><span style='font-size: 16px; line-height: 30.4px;'>Promemoria: domani hai la lezione $servicename del $dataformat.</span></p>
|
||||
<p style='font-size: 14px; line-height: 190%;'><span style='font-size: 16px; line-height: 30.4px;'>Puoi cancellarla fino alle 12:00 cliccando qui:</span></p>
|
||||
<a href='$link' target='_blank'>Cancella prenotazione</a>
|
||||
<br>
|
||||
<p style='font-size: 14px; line-height: 190%;'><span style='font-size: 16px; line-height: 30.4px;'>Ci vediamo sul tappetino!</span></p>
|
||||
<p style='font-size: 14px; line-height: 190%;'><span style='font-size: 16px; line-height: 30.4px;'>Il Team Yogasoul</span></p>";
|
||||
|
||||
// Definisci $messageedit per il template
|
||||
$messageedit = $message;
|
||||
|
||||
// Definisci $buttonedit (vuoto o pulsante generico)
|
||||
$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>";
|
||||
|
||||
require 'phpmailer/src/Exception.php';
|
||||
require 'phpmailer/src/PHPMailer.php';
|
||||
require 'phpmailer/src/SMTP.php';
|
||||
|
||||
$mail = new PHPMailer(true);
|
||||
try {
|
||||
$mail->isSMTP();
|
||||
$mail->Host = 'mail.yogasoul.it';
|
||||
$mail->SMTPAuth = true;
|
||||
$mail->Username = 'info@yogasoul.it';
|
||||
$mail->Password = '!Testolina88';
|
||||
$mail->SMTPSecure = 'tls';
|
||||
$mail->Port = 587;
|
||||
|
||||
// Verifica esistenza file template
|
||||
if (!file_exists('mail/emailtemplate2.php')) {
|
||||
throw new Exception("File emailtemplate2.php non trovato.");
|
||||
}
|
||||
include('mail/emailtemplate2.php');
|
||||
|
||||
// Verifica che $mailmessage1 esista
|
||||
if (!isset($mailmessage1)) {
|
||||
throw new Exception("Variabile \$mailmessage1 non definita in emailtemplate2.php.");
|
||||
}
|
||||
|
||||
// Sostituisci placeholder (anche se non usato, per compatibilità)
|
||||
$htmlContent = str_replace('{message}', $message, $mailmessage1);
|
||||
|
||||
$mail->From = 'info@yogasoul.it';
|
||||
$mail->FromName = 'YogiBook [YogaSoul]';
|
||||
$mail->addAddress($emailuser);
|
||||
$mail->Subject = "YogiBook - Promemoria lezione domani!";
|
||||
$mail->Body = $htmlContent;
|
||||
$mail->AltBody = 'Promemoria lezione.';
|
||||
|
||||
$mail->send();
|
||||
$emailCount++;
|
||||
$logMessage .= "Email inviata a $emailuser per lezione ID $idbookingclass ($dataformat)\n";
|
||||
} catch (Exception $e) {
|
||||
$errors[] = "Errore invio email a $emailuser (ID $idbookingclass): " . $mail->ErrorInfo;
|
||||
$logMessage .= "Errore invio a $emailuser (ID $idbookingclass): " . $mail->ErrorInfo . "\n";
|
||||
}
|
||||
|
||||
sleep(2); // Delay 2 secondi
|
||||
}
|
||||
|
||||
// Scrivi log
|
||||
file_put_contents($logFile, $logMessage, FILE_APPEND);
|
||||
|
||||
// Output debug
|
||||
echo "Esecuzione completata: $emailCount email inviate.\n";
|
||||
if (!empty($errors)) {
|
||||
echo "Errori rilevati:\n";
|
||||
foreach ($errors as $error) {
|
||||
echo "- $error\n";
|
||||
}
|
||||
} else {
|
||||
echo "Nessun errore.\n";
|
||||
}
|
||||
echo "Dettagli nel file di log: $logFile\n";
|
||||
|
||||
$conn->close();
|
||||
Reference in New Issue
Block a user