0) { $sql .= ' AND a.webinar_id = ?'; $params[] = $selectedWebinarId; } $sql .= ' ORDER BY a.created_at DESC, a.id DESC'; $stmt = $pdo->prepare($sql); $stmt->execute($params); $attendees = $stmt->fetchAll(PDO::FETCH_ASSOC); if (!$attendees) { admin_set_flash('No active attendees were found for the selected webinar.'); header('Location: admin.php' . ($selectedWebinarId > 0 ? '?webinar_id=' . urlencode((string) $selectedWebinarId) : '')); exit; } $sentCount = 0; $failedCount = 0; foreach ($attendees as $attendee) { $payload = webinar_build_email_payload((string) ($attendee['first_name'] ?? ''), $attendee, true); $result = MailService::sendMail((string) $attendee['email'], $payload['subject'], $payload['html'], $payload['text']); if (!empty($result['success'])) { $sentCount++; } else { $failedCount++; error_log('Correction email failed for attendee #' . (int) $attendee['id'] . ': ' . ($result['error'] ?? 'unknown error')); } } $scope = $selectedWebinarId > 0 ? 'selected webinar' : 'all active webinars'; $message = "Correction email sent to {$sentCount} attendee(s) for the {$scope}."; if ($failedCount > 0) { $message .= " {$failedCount} email(s) failed to send. Check the mail log for details."; } admin_set_flash($message); } catch (Throwable $e) { error_log('Failed to send correction emails: ' . $e->getMessage()); admin_set_flash('Could not send the correction email right now. Please try again.'); } header('Location: admin.php' . ($selectedWebinarId > 0 ? '?webinar_id=' . urlencode((string) $selectedWebinarId) : '')); exit;