Rundmail-Versand blockweise aus dem Browser statt in einem Request

Bei ~150 Empfängern lief der synchrone SMTP-Versand in die Zeitlimits des
Shared Hosts; bei Abbruch gingen bereits verschickte Mails ins Leere und der
Protokolleintrag fehlte. Jetzt verschickt jeder Aufruf 10 Mails, der
Fortschritt liegt in der Session und wird pro Empfänger gesichert. Die
Fortschrittsseite stößt die Blöcke per fetch an, ein offener Rest lässt sich
auf der Übersicht fortsetzen. Ein Cron-Job ist dafür nicht nötig.
This commit is contained in:
torsten 2026-11-30 12:39:00 +01:00
parent 8ff2287419
commit 744a68fcdb
5 changed files with 181 additions and 22 deletions

View file

@ -805,6 +805,81 @@ if ($page === 'reminder') {
Auth::requireLogin();
Auth::requireAdmin();
// Versand erfolgt blockweise aus dem Browser: Ein Request verschickt nur
// BATCH_SIZE Mails. So bleibt jeder Aufruf deutlich unter den Zeitlimits
// des Shared Hosts, und ein Abbruch verliert höchstens einen Block.
$jobKey = 'roundmail_job';
$batchSize = 10;
if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'batch') {
@set_time_limit(120);
ignore_user_abort(true);
$wantsJson = ($_POST['format'] ?? '') === 'json';
$job = $_SESSION[$jobKey] ?? null;
if (!$job) {
if ($wantsJson) {
header('Content-Type: application/json');
echo json_encode(['done' => true, 'redirect' => '/index.php?page=admin_roundmails']);
exit;
}
header('Location: /index.php?page=admin_roundmails');
exit;
}
$batch = array_splice($job['queue'], 0, $batchSize);
$error = null;
try {
Mailer::send($batch, $job['topic'], $job['html'], $job['reply_to'],
function ($email, $ok) use (&$job, $jobKey) {
$job[$ok ? 'sent' : 'failed'][] = $email;
$_SESSION[$jobKey] = $job;
// Fortschritt sofort sichern: bricht der Request ab, ist er erhalten
session_write_close();
session_start();
});
} catch (\Throwable $e) {
// Der Block gilt als nicht versendet und wird beim nächsten Aufruf wiederholt
$job['queue'] = array_merge($batch, $job['queue']);
$error = $e->getMessage();
}
$total = count($job['queue']) + count($job['sent']) + count($job['failed']);
$done = empty($job['queue']);
$redirect = '/index.php?page=admin_roundmails';
if ($done) {
if (!empty($job['sent'])) {
Roundmail::create((int)$job['user_id'], $job['sent'], $job['topic'], $job['html'], (int)$job['season_id']);
}
$_SESSION['admin_message'] = 'Mail an ' . count($job['sent']) . ' Empfänger versendet.';
if (!empty($job['failed'])) {
$_SESSION['admin_error'] = 'Nicht zustellbar: ' . implode(', ', $job['failed']);
}
unset($_SESSION[$jobKey]);
} else {
$_SESSION[$jobKey] = $job;
$redirect = '/index.php?page=admin_roundmails&send=1';
}
if ($wantsJson) {
header('Content-Type: application/json');
echo json_encode([
'done' => $done,
'sent' => count($job['sent']),
'failed' => count($job['failed']),
'total' => $total,
'remaining' => count($job['queue']),
'error' => $error,
'redirect' => $redirect,
]);
exit;
}
header('Location: ' . $redirect);
exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$redirect = '/index.php?page=admin_roundmails';
try {
@ -823,17 +898,18 @@ if ($page === 'reminder') {
$sender = Auth::user();
$currentSeason = Season::getViewSeason();
$html = Roundmail::textToHtml($text);
$failed = Mailer::send($recipients['valid'], $topic, $html, $sender['email'] ?? null);
$sent = array_values(array_diff($recipients['valid'], $failed));
if (!empty($sent)) {
Roundmail::create((int)$sender['id'], $sent, $topic, $html, $currentSeason ? (int)$currentSeason['id'] : 0);
}
$_SESSION['admin_message'] = 'Mail an ' . count($sent) . ' Empfänger versendet.';
if (!empty($failed)) {
$_SESSION['admin_error'] = 'Nicht zustellbar: ' . implode(', ', $failed);
}
$_SESSION[$jobKey] = [
'user_id' => (int)$sender['id'],
'reply_to' => $sender['email'] ?? null,
'topic' => $topic,
'html' => Roundmail::textToHtml($text),
'season_id' => $currentSeason ? (int)$currentSeason['id'] : 0,
'queue' => array_values($recipients['valid']),
'sent' => [],
'failed' => [],
];
$_SESSION['admin_message'] = 'Versand läuft: ' . count($recipients['valid']) . ' Empfänger.';
$redirect = '/index.php?page=admin_roundmails&send=1';
} catch (\Throwable $e) {
$_SESSION['admin_error'] = 'Fehler: ' . $e->getMessage();
$_SESSION['admin_form'] = $_POST;
@ -852,6 +928,13 @@ if ($page === 'reminder') {
$compose = isset($_GET['new']);
$excludeReminders = ($_GET['hide_reminders'] ?? '') === '1';
// Offener Versand aus einer früheren Sitzung (Abbruch, Timeout, Browser zu)
$sendJob = $_SESSION[$jobKey] ?? null;
$sending = isset($_GET['send']) && $sendJob;
if ($sending) {
$sendTotal = count($sendJob['queue']) + count($sendJob['sent']) + count($sendJob['failed']);
}
if ($compose) {
$currentSeason = Season::getViewSeason();
$tippableRound = $currentSeason ? Round::getFirstTippable($currentSeason['id']) : null;
@ -859,7 +942,7 @@ if ($page === 'reminder') {
$baseUrl = rtrim(getenv('APP_URL') ?: ((!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . ($_SERVER['HTTP_HOST'] ?? 'localhost')), '/');
$tippLink = $baseUrl . '/';
$mailConfigured = (bool)getenv('SMTP_HOST');
} elseif (!$viewMail) {
} elseif (!$viewMail && !$sending) {
$perPage = 20;
$totalMails = Roundmail::count($excludeReminders);
$totalPages = max(1, (int)ceil($totalMails / $perPage));