prepare("SELECT b.*, c.name as client_name, c.phone as client_phone FROM bookings b JOIN clients c ON b.client_id = c.id WHERE b.start_time BETWEEN NOW() AND NOW() + INTERVAL 24 HOUR AND b.status = 'confirmed'"); $stmt->execute(); $bookings = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($bookings as $booking) { if (empty($booking['client_phone'])) { continue; } $message = "Hi " . $booking['client_name'] . ", this is a reminder for your upcoming session tomorrow at " . date("g:i a", strtotime($booking['start_time'])) . "."; try { $response = \Telnyx\Message::create([ 'from' => TELNYX_MESSAGING_PROFILE_ID, 'to' => $booking['client_phone'], 'text' => $message, ]); // Log the message $log_stmt = db()->prepare("INSERT INTO sms_logs (client_id, sender, recipient, message, status) VALUES (?, ?, ?, ?, ?, ?)"); $log_stmt->execute([ $booking['client_id'], TELNYX_MESSAGING_PROFILE_ID, $booking['client_phone'], $message, 'sent' ]); echo "Reminder sent to " . $booking['client_name'] . "\n"; } catch (\Telnyx\Exception\ApiErrorException $e) { echo "Error sending reminder to " . $booking['client_name'] . ": " . $e->getMessage() . "\n"; // Log the failure $log_stmt = db()->prepare("INSERT INTO sms_logs (client_id, sender, recipient, message, status) VALUES (?, ?, ?, ?, ?, ?)"); $log_stmt->execute([ $booking['client_id'], TELNYX_MESSAGING_PROFILE_ID, $booking['client_phone'], $message, 'failed' ]); } }