2026-11-30 08:11:06 +01:00
|
|
|
|
<?php
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Kundenrundmail (z. B. Dankeschön zum Jahresende).
|
|
|
|
|
|
*
|
|
|
|
|
|
* Empfänger werden aus den Buchungen gezogen: jede Buchung speichert bereits
|
|
|
|
|
|
* eine E-Mail-Adresse im Feld bookings.email. Es gibt deshalb keine eigene
|
|
|
|
|
|
* Kundenliste – die Empfängerliste ist immer der aktuelle Stand der Buchungen,
|
|
|
|
|
|
* abzüglich der Adressen, die sich abgemeldet haben (mail_optouts).
|
|
|
|
|
|
*
|
2026-11-30 15:40:34 +01:00
|
|
|
|
* Angeschrieben werden Kunden mit einer Buchung, die in den letzten 13 Monaten
|
|
|
|
|
|
* begonnen hat und nicht storniert ist (`bookings.start_date` zwischen heute
|
|
|
|
|
|
* und heute minus 13 Monaten, `bookings.status = 'active'`). Ob die Mietzeit
|
|
|
|
|
|
* schon vorbei ist, spielt keine Rolle – eine Buchung von Anfang November bis
|
|
|
|
|
|
* Mitte Januar zählt also mit. Stornierte Buchungen bleiben außen vor; die
|
|
|
|
|
|
* Buchungsbestätigung selbst ist davon unberührt.
|
|
|
|
|
|
*
|
2026-11-30 08:11:06 +01:00
|
|
|
|
* Versand läuft in Häppchen (mailing_send_batch), damit Shared-Hosting-Limits
|
|
|
|
|
|
* und Timeouts nicht zuschlagen. Jede Mail geht einzeln raus (kein BCC), damit
|
|
|
|
|
|
* Zustellbarkeit und persönliche Anrede stimmen. Der Stand steht je Empfänger
|
|
|
|
|
|
* in mailing_recipients.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
const MAILING_BATCH_DEFAULT = 25;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-11-30 15:40:34 +01:00
|
|
|
|
* Empfängerliste: alle Kunden mit gültiger Adresse in einer nicht stornierten
|
|
|
|
|
|
* Buchung der letzten 13 Monate, ohne Abmeldungen. Sortiert nach Adresse, damit
|
|
|
|
|
|
* der Versand reproduzierbar ist.
|
2026-11-30 08:11:06 +01:00
|
|
|
|
*
|
|
|
|
|
|
* @return list<array{email: string, bookings: int, last_booking: ?string}>
|
|
|
|
|
|
*/
|
|
|
|
|
|
function mailing_recipients(): array
|
|
|
|
|
|
{
|
|
|
|
|
|
$sql = "SELECT LOWER(b.email) AS email,
|
|
|
|
|
|
COUNT(*) AS bookings,
|
|
|
|
|
|
MAX(b.start_date) AS last_booking
|
|
|
|
|
|
FROM bookings b
|
|
|
|
|
|
WHERE b.email <> ''
|
2026-11-30 15:40:34 +01:00
|
|
|
|
AND b.status = 'active'
|
|
|
|
|
|
AND b.start_date <= CURDATE()
|
|
|
|
|
|
AND b.start_date >= CURDATE() - INTERVAL 13 MONTH
|
2026-11-30 08:11:06 +01:00
|
|
|
|
AND NOT EXISTS (SELECT 1 FROM mail_optouts o WHERE o.email = LOWER(b.email))
|
|
|
|
|
|
GROUP BY LOWER(b.email)
|
|
|
|
|
|
ORDER BY email";
|
|
|
|
|
|
$rows = [];
|
|
|
|
|
|
foreach (db()->query($sql)->fetchAll() as $row) {
|
|
|
|
|
|
$email = (string) $row['email'];
|
|
|
|
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
$rows[] = [
|
|
|
|
|
|
'email' => $email,
|
|
|
|
|
|
'bookings' => (int) $row['bookings'],
|
|
|
|
|
|
'last_booking' => $row['last_booking'] !== null ? (string) $row['last_booking'] : null,
|
|
|
|
|
|
];
|
|
|
|
|
|
}
|
|
|
|
|
|
return $rows;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @return list<string> */
|
|
|
|
|
|
function mailing_optout_emails(): array
|
|
|
|
|
|
{
|
|
|
|
|
|
return array_map('strval', db()->query('SELECT email FROM mail_optouts ORDER BY email')
|
|
|
|
|
|
->fetchAll(PDO::FETCH_COLUMN));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function mailing_optout_count(): int
|
|
|
|
|
|
{
|
|
|
|
|
|
return (int) db()->query('SELECT COUNT(*) FROM mail_optouts')->fetchColumn();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function mailing_is_optout(string $email): bool
|
|
|
|
|
|
{
|
|
|
|
|
|
try {
|
|
|
|
|
|
$stmt = db()->prepare('SELECT 1 FROM mail_optouts WHERE email = ? LIMIT 1');
|
|
|
|
|
|
$stmt->execute([mb_strtolower(trim($email))]);
|
|
|
|
|
|
return (bool) $stmt->fetchColumn();
|
|
|
|
|
|
} catch (Throwable) {
|
|
|
|
|
|
// Fehlende Migration darf die öffentliche Abmeldeseite nicht sprengen.
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function mailing_optout_add(string $email, string $source = 'link'): bool
|
|
|
|
|
|
{
|
|
|
|
|
|
$email = mb_strtolower(trim($email));
|
|
|
|
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
|
|
|
$stmt = db()->prepare('INSERT IGNORE INTO mail_optouts (email, source, created_at) VALUES (?, ?, ?)');
|
|
|
|
|
|
return $stmt->execute([$email, mb_substr($source, 0, 40), utc_now()]);
|
|
|
|
|
|
} catch (Throwable) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Signatur für Abmeldelinks: HMAC über die Adresse, damit Links nicht ratbar sind. */
|
|
|
|
|
|
function mailing_unsub_token(string $email): string
|
|
|
|
|
|
{
|
|
|
|
|
|
$secret = (string) config('app.cron_token', '');
|
|
|
|
|
|
if ($secret === '') {
|
|
|
|
|
|
$secret = (string) config('app.install_token', 'drink-it-cold');
|
|
|
|
|
|
}
|
|
|
|
|
|
return hash_hmac('sha256', mb_strtolower(trim($email)), $secret);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function mailing_unsub_url(string $email): string
|
|
|
|
|
|
{
|
|
|
|
|
|
$base = rtrim((string) config('app.public_url', ''), '/');
|
|
|
|
|
|
return $base . '/abmelden?e=' . rawurlencode(mb_strtolower(trim($email)))
|
|
|
|
|
|
. '&t=' . mailing_unsub_token($email);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function mailing_unsub_valid(string $email, string $token): bool
|
|
|
|
|
|
{
|
|
|
|
|
|
return $token !== '' && hash_equals(mailing_unsub_token($email), $token);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-30 15:40:34 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Ersetzt Platzhalter im Text – im Betreff wie im Rumpf: {jahr} wird zum
|
|
|
|
|
|
* laufenden Jahr. Betreff und Text werden erst beim Versand (und in der
|
|
|
|
|
|
* Übersicht) gerendert, in der Datenbank bleibt der Platzhalter stehen.
|
|
|
|
|
|
*/
|
|
|
|
|
|
function mailing_render(string $text): string
|
|
|
|
|
|
{
|
|
|
|
|
|
return str_replace('{jahr}', date('Y'), $text);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-30 08:11:06 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Setzt den Text zusammen: Admin-Text plus Pflicht-Fußzeile mit Abmeldelink.
|
|
|
|
|
|
* {jahr} wird durch das laufende Jahr ersetzt.
|
|
|
|
|
|
*/
|
|
|
|
|
|
function mailing_body_for(string $body, string $email): string
|
|
|
|
|
|
{
|
2026-11-30 15:40:34 +01:00
|
|
|
|
$body = mailing_render(trim($body));
|
2026-11-30 08:11:06 +01:00
|
|
|
|
$footer = "—\n"
|
|
|
|
|
|
. "Sie erhalten diese Nachricht, weil Sie bei Drink it cold einen Kühlanhänger gebucht haben.\n"
|
|
|
|
|
|
. 'Keine solchen Nachrichten mehr? Hier abmelden: ' . mailing_unsub_url($email);
|
|
|
|
|
|
return $body . "\n\n" . $footer . "\n";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Legt eine Kampagne an und friert die aktuelle Empfängerliste ein.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return array{id: int, count: int}
|
|
|
|
|
|
*/
|
|
|
|
|
|
function mailing_create(string $subject, string $body, ?int $userId): array
|
|
|
|
|
|
{
|
|
|
|
|
|
$subject = trim($subject);
|
|
|
|
|
|
$body = trim($body);
|
|
|
|
|
|
$recipients = mailing_recipients();
|
|
|
|
|
|
|
|
|
|
|
|
$pdo = db();
|
|
|
|
|
|
$pdo->beginTransaction();
|
|
|
|
|
|
try {
|
|
|
|
|
|
$stmt = $pdo->prepare('INSERT INTO mailings (subject, body, recipient_count, created_at, created_by) VALUES (?, ?, ?, ?, ?)');
|
|
|
|
|
|
$stmt->execute([$subject, $body, count($recipients), utc_now(), $userId]);
|
|
|
|
|
|
$id = (int) $pdo->lastInsertId();
|
|
|
|
|
|
|
|
|
|
|
|
$insert = $pdo->prepare("INSERT IGNORE INTO mailing_recipients (mailing_id, email, status) VALUES (?, ?, 'pending')");
|
|
|
|
|
|
foreach ($recipients as $recipient) {
|
|
|
|
|
|
$insert->execute([$id, $recipient['email']]);
|
|
|
|
|
|
}
|
|
|
|
|
|
$pdo->commit();
|
|
|
|
|
|
} catch (Throwable $e) {
|
|
|
|
|
|
$pdo->rollBack();
|
|
|
|
|
|
throw $e;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ['id' => $id, 'count' => count($recipients)];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @return list<array<string, mixed>> */
|
|
|
|
|
|
function mailing_list(int $limit = 20): array
|
|
|
|
|
|
{
|
|
|
|
|
|
$stmt = db()->prepare('SELECT m.*, u.first_name, u.last_name
|
|
|
|
|
|
FROM mailings m LEFT JOIN users u ON u.id = m.created_by
|
|
|
|
|
|
ORDER BY m.id DESC LIMIT ' . (int) $limit);
|
|
|
|
|
|
$stmt->execute();
|
|
|
|
|
|
return $stmt->fetchAll();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @return array<string, mixed>|null */
|
|
|
|
|
|
function mailing_find(int $id): ?array
|
|
|
|
|
|
{
|
|
|
|
|
|
$stmt = db()->prepare('SELECT * FROM mailings WHERE id = ? LIMIT 1');
|
|
|
|
|
|
$stmt->execute([$id]);
|
|
|
|
|
|
$row = $stmt->fetch();
|
|
|
|
|
|
return $row ?: null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function mailing_pending_count(int $id): int
|
|
|
|
|
|
{
|
|
|
|
|
|
$stmt = db()->prepare("SELECT COUNT(*) FROM mailing_recipients WHERE mailing_id = ? AND status = 'pending'");
|
|
|
|
|
|
$stmt->execute([$id]);
|
|
|
|
|
|
return (int) $stmt->fetchColumn();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function mailing_refresh_counts(int $id): void
|
|
|
|
|
|
{
|
|
|
|
|
|
$stmt = db()->prepare("UPDATE mailings SET
|
|
|
|
|
|
sent_count = (SELECT COUNT(*) FROM mailing_recipients WHERE mailing_id = ? AND status = 'sent'),
|
|
|
|
|
|
failed_count = (SELECT COUNT(*) FROM mailing_recipients WHERE mailing_id = ? AND status = 'failed'),
|
|
|
|
|
|
finished_at = CASE WHEN (SELECT COUNT(*) FROM mailing_recipients WHERE mailing_id = ? AND status = 'pending') = 0
|
|
|
|
|
|
THEN COALESCE(finished_at, ?) ELSE NULL END
|
|
|
|
|
|
WHERE id = ?");
|
|
|
|
|
|
$stmt->execute([$id, $id, $id, utc_now(), $id]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-30 15:40:34 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Protokoll je Empfänger einer Kampagne: Status, Versandzeitpunkt und
|
|
|
|
|
|
* Fehlergrund. Fehlgeschlagene und offene Einträge stehen oben.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return list<array{email: string, status: string, sent_at: ?string, error: string}>
|
|
|
|
|
|
*/
|
|
|
|
|
|
function mailing_recipient_log(int $id, int $limit = 1000): array
|
|
|
|
|
|
{
|
|
|
|
|
|
$stmt = db()->prepare(
|
|
|
|
|
|
"SELECT email, status, sent_at, error FROM mailing_recipients
|
|
|
|
|
|
WHERE mailing_id = ?
|
|
|
|
|
|
ORDER BY (status = 'sent'), email
|
|
|
|
|
|
LIMIT " . max(1, $limit)
|
|
|
|
|
|
);
|
|
|
|
|
|
$stmt->execute([$id]);
|
|
|
|
|
|
return $stmt->fetchAll();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-30 08:11:06 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Verschickt die nächsten offenen Empfänger einer Kampagne.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return array{sent: int, failed: int, remaining: int}
|
|
|
|
|
|
*/
|
|
|
|
|
|
function mailing_send_batch(int $id, int $limit = MAILING_BATCH_DEFAULT): array
|
|
|
|
|
|
{
|
|
|
|
|
|
$mailing = mailing_find($id);
|
|
|
|
|
|
if (!$mailing) {
|
|
|
|
|
|
return ['sent' => 0, 'failed' => 0, 'remaining' => 0];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$limit = max(1, min(100, $limit));
|
|
|
|
|
|
$pdo = db();
|
|
|
|
|
|
$stmt = $pdo->prepare("SELECT email FROM mailing_recipients
|
|
|
|
|
|
WHERE mailing_id = ? AND status = 'pending' ORDER BY email LIMIT " . $limit);
|
|
|
|
|
|
$stmt->execute([$id]);
|
|
|
|
|
|
$emails = array_map('strval', $stmt->fetchAll(PDO::FETCH_COLUMN));
|
|
|
|
|
|
|
|
|
|
|
|
$markSent = $pdo->prepare("UPDATE mailing_recipients SET status = 'sent', sent_at = ?, error = '' WHERE mailing_id = ? AND email = ?");
|
|
|
|
|
|
$markFailed = $pdo->prepare("UPDATE mailing_recipients SET status = 'failed', error = ? WHERE mailing_id = ? AND email = ?");
|
|
|
|
|
|
|
|
|
|
|
|
$sent = 0;
|
|
|
|
|
|
$failed = 0;
|
|
|
|
|
|
foreach ($emails as $email) {
|
|
|
|
|
|
// Abmeldungen, die nach dem Anlegen der Kampagne kamen, respektieren.
|
|
|
|
|
|
if (mailing_is_optout($email)) {
|
|
|
|
|
|
$markFailed->execute(['abgemeldet', $id, $email]);
|
|
|
|
|
|
$failed++;
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2026-11-30 15:40:34 +01:00
|
|
|
|
$ok = mail_send($email, mailing_render((string) $mailing['subject']), mailing_body_for((string) $mailing['body'], $email));
|
2026-11-30 08:11:06 +01:00
|
|
|
|
if ($ok) {
|
|
|
|
|
|
$markSent->execute([utc_now(), $id, $email]);
|
|
|
|
|
|
$sent++;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
$markFailed->execute(['Versand fehlgeschlagen', $id, $email]);
|
|
|
|
|
|
$failed++;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mailing_refresh_counts($id);
|
|
|
|
|
|
return ['sent' => $sent, 'failed' => $failed, 'remaining' => mailing_pending_count($id)];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Testmail an eine eigene Adresse – ohne Eintrag in einer Kampagne. */
|
|
|
|
|
|
function mailing_send_test(string $subject, string $body, string $to): bool
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!filter_var($to, FILTER_VALIDATE_EMAIL)) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2026-11-30 15:40:34 +01:00
|
|
|
|
return mail_send($to, '[Test] ' . mailing_render(trim($subject)), mailing_body_for($body, $to));
|
2026-11-30 08:11:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Öffentliche Abmeldeseite /abmelden?e=…&t=…
|
|
|
|
|
|
* GET zeigt die Bestätigung, POST trägt die Adresse in die Sperrliste ein.
|
|
|
|
|
|
*/
|
|
|
|
|
|
function page_unsubscribe(string $method): void
|
|
|
|
|
|
{
|
|
|
|
|
|
$email = mb_strtolower(trim(query_string('e')));
|
|
|
|
|
|
$token = query_string('t');
|
|
|
|
|
|
$valid = filter_var($email, FILTER_VALIDATE_EMAIL) && mailing_unsub_valid($email, $token);
|
|
|
|
|
|
|
|
|
|
|
|
$done = false;
|
|
|
|
|
|
$error = '';
|
|
|
|
|
|
if ($method === 'POST') {
|
|
|
|
|
|
$email = mb_strtolower(trim(post_string('e')));
|
|
|
|
|
|
$token = post_string('t');
|
|
|
|
|
|
$valid = filter_var($email, FILTER_VALIDATE_EMAIL) && mailing_unsub_valid($email, $token);
|
|
|
|
|
|
if (!$valid) {
|
|
|
|
|
|
$error = 'Dieser Abmeldelink ist ungültig oder unvollständig.';
|
|
|
|
|
|
} else {
|
|
|
|
|
|
mailing_optout_add($email, 'abmeldelink');
|
|
|
|
|
|
$done = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
render_standalone('mailing_abmelden', [
|
|
|
|
|
|
'email' => $email,
|
|
|
|
|
|
'token' => $token,
|
|
|
|
|
|
'valid' => $valid,
|
|
|
|
|
|
'done' => $done,
|
|
|
|
|
|
'error' => $error,
|
|
|
|
|
|
'already' => $valid && !$done && mailing_is_optout($email),
|
|
|
|
|
|
], 'Rundmail abbestellen');
|
|
|
|
|
|
}
|
2026-11-30 08:19:22 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Vorlage für die Jahresend-Rundmail. Der Textkörper ist reiner Text; Platzhalter
|
|
|
|
|
|
* {jahr} wird beim Versand ersetzt, Herkunftshinweis und Abmeldelink hängt
|
|
|
|
|
|
* mailing_body_for() automatisch an.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return array{subject: string, body: string}
|
|
|
|
|
|
*/
|
|
|
|
|
|
function mailing_template(): array
|
|
|
|
|
|
{
|
2026-11-30 08:22:49 +01:00
|
|
|
|
$subject = 'Vielen Dank für {jahr} – und rechtzeitig reservieren für die neue Saison!';
|
2026-11-30 08:19:22 +01:00
|
|
|
|
$body = "Hallo liebe Kundin, hallo lieber Kunde,\n"
|
|
|
|
|
|
. "\n"
|
|
|
|
|
|
. "wir sagen Danke: Sie haben {jahr} einen unserer Kühlanhänger gemietet – vielen Dank für Ihr Vertrauen!\n"
|
|
|
|
|
|
. "\n"
|
|
|
|
|
|
. "Damit Sie für Ihre Feier, Ihr Fest oder Ihren Betrieb auch nächstes Jahr den passenden Termin bekommen,\n"
|
|
|
|
|
|
. "denken Sie bitte frühzeitig an Ihre Reservierung. Gerade an Sonn- und Feiertagen sowie in den\n"
|
|
|
|
|
|
. "Sommermonaten sind unsere Anhänger schnell ausgebucht.\n"
|
|
|
|
|
|
. "\n"
|
|
|
|
|
|
. "Sie erreichen uns wie gewohnt telefonisch oder über unsere Internetseite – wir freuen uns auf Sie.\n"
|
|
|
|
|
|
. "\n"
|
|
|
|
|
|
. "Herzliche Grüße\n"
|
|
|
|
|
|
. "Ihr Team von Drink it cold";
|
|
|
|
|
|
return ['subject' => $subject, 'body' => $body];
|
|
|
|
|
|
}
|