74 lines
2.5 KiB
PHP
74 lines
2.5 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
require_once 'mail/MailService.php';
|
|
require_once 'includes/admin_auth.php';
|
|
require_once 'includes/webinar_schedule.php';
|
|
require_once 'includes/webinar_email.php';
|
|
|
|
admin_require_login();
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
header('Location: admin.php');
|
|
exit;
|
|
}
|
|
|
|
$selectedWebinarId = filter_input(INPUT_POST, 'webinar_id', FILTER_VALIDATE_INT);
|
|
$selectedWebinarId = $selectedWebinarId ? max(0, (int) $selectedWebinarId) : 0;
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
$sql = "SELECT a.id, a.first_name, a.last_name, a.email, a.webinar_id, w.title, w.description, w.scheduled_at, w.presenter
|
|
FROM attendees a
|
|
INNER JOIN webinars w ON w.id = a.webinar_id
|
|
WHERE a.deleted_at IS NULL";
|
|
$params = [];
|
|
|
|
if ($selectedWebinarId > 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;
|