feat(mailing): Kundenrundmail mit Empfängern aus Buchungen, Abmeldelink und Versandprotokoll

Empfänger kommen aus bookings.email (dedupliziert, ohne Abmeldungen), Text mit
{jahr}, Testmail an die eigene Adresse, Versand in Häppchen über mail_send().
Abmeldungen landen in mail_optouts und gelten für alle künftigen Rundmails.
Migration 009_mailing.sql, DSGVO-Verzeichnis und ROADMAP (Feature 18) ergänzt.
This commit is contained in:
torsten 2026-11-30 08:11:06 +01:00
parent 1adc3c8f33
commit 5cce6ca86f
11 changed files with 657 additions and 1 deletions

View file

@ -42,6 +42,10 @@ function admin_handle(string $path, array $user): void
admin_reminders_send();
return;
}
if ($path === '/admin/mailings') {
admin_mailings($user);
return;
}
abort_page(404, 'Diese Verwaltungsseite gibt es nicht.');
}
@ -586,3 +590,84 @@ function admin_reminders_send(): void
flash($mail['errors'] ? 'error' : 'success', implode(' ', $parts));
redirect('/reminders');
}
/**
* Kundenrundmail: Empfänger aus den Buchungen, Text mit {jahr}, Testmail und
* Versand in Häppchen. Abmeldungen landen in mail_optouts (app/mailing.php).
*/
function admin_mailings(array $user): void
{
$values = ['subject' => '', 'body' => ''];
$errors = [];
if (is_post()) {
require_post();
$action = post_string('action');
if ($action === 'test' || $action === 'create') {
$values['subject'] = trim(post_string('subject'));
$values['body'] = trim(post_string('body'));
if (mb_strlen($values['subject']) < 3 || mb_strlen($values['subject']) > 190) {
$errors['subject'] = 'Bitte einen Betreff mit 3 bis 190 Zeichen angeben.';
}
if (mb_strlen($values['body']) < 10 || mb_strlen($values['body']) > 5000) {
$errors['body'] = 'Bitte einen Text mit 10 bis 5000 Zeichen angeben.';
}
if (!$errors && !mail_is_enabled()) {
$errors['body'] = 'Der Mailversand ist in der Konfiguration ausgeschaltet (app.mail_enabled).';
}
if (!$errors && $action === 'test') {
$to = (string) ($user['email'] ?? '');
$ok = mailing_send_test($values['subject'], $values['body'], $to);
flash($ok ? 'success' : 'error', $ok
? 'Testmail an ' . $to . ' verschickt (mit Abmeldelink im Text).'
: 'Testmail konnte nicht verschickt werden – bitte Absenderadresse prüfen.');
redirect('/admin/mailings');
}
if (!$errors && $action === 'create') {
$created = mailing_create($values['subject'], $values['body'], (int) $user['id']);
if ($created['count'] === 0) {
flash('error', 'Es gibt derzeit keine Empfänger: keine Buchung mit gültiger E-Mail-Adresse.');
} else {
flash('success', 'Rundmail angelegt – ' . $created['count']
. ' Empfänger. Der Versand läuft in Häppchen, bis alle durch sind.');
}
redirect('/admin/mailings');
}
}
if ($action === 'send') {
$id = (int) post_string('mailing_id');
$mailing = $id > 0 ? mailing_find($id) : null;
if (!$mailing) {
abort_page(404, 'Diese Rundmail gibt es nicht.');
}
if (!mail_is_enabled()) {
flash('error', 'Der Mailversand ist in der Konfiguration ausgeschaltet (app.mail_enabled).');
redirect('/admin/mailings');
}
$result = mailing_send_batch($id, (int) post_string('batch', (string) MAILING_BATCH_DEFAULT));
$message = sprintf('%d verschickt, %d fehlgeschlagen, %d offen.',
$result['sent'], $result['failed'], $result['remaining']);
flash($result['failed'] > 0 ? 'info' : 'success', $message);
redirect('/admin/mailings');
}
}
$mailings = mailing_list();
foreach ($mailings as $index => $mailing) {
$mailings[$index]['pending'] = mailing_pending_count((int) $mailing['id']);
}
render_page('admin_mailings', [
'recipients' => mailing_recipients(),
'optoutCount' => mailing_optout_count(),
'mailings' => $mailings,
'values' => $values,
'errors' => $errors,
'mailEnabled' => mail_is_enabled(),
'batchSize' => MAILING_BATCH_DEFAULT,
], 'Kundenrundmail');
}