query( 'SELECT mail_host, mail_port, mail_username, mail_password, mail_encryption, mail_from_email, mail_from_name, mail_notify_email, mail_customer_subject, mail_customer_body, mail_owner_subject, mail_owner_body FROM app_settings WHERE id = 1' ); $row = $stmt->fetch() ?: []; } catch (\Throwable $e) { $row = []; } $str = fn($value) => $value !== null && $value !== '' ? $value : null; return [ 'host' => $str($row['mail_host'] ?? null) ?? $fallback['host'], 'port' => $str($row['mail_port'] ?? null) ?? $fallback['port'], 'username' => $str($row['mail_username'] ?? null) ?? $fallback['username'], 'password' => $str($row['mail_password'] ?? null) ?? $fallback['password'], 'encryption' => $str($row['mail_encryption'] ?? null) ?? $fallback['encryption'], 'from_email' => $str($row['mail_from_email'] ?? null) ?? $fallback['from_email'], 'from_name' => $str($row['mail_from_name'] ?? null) ?? $fallback['from_name'], 'notify_email' => $str($row['mail_notify_email'] ?? null) ?? $fallback['to_email'], 'customer_subject' => $str($row['mail_customer_subject'] ?? null) ?? self::DEFAULT_CUSTOMER_SUBJECT, 'customer_body' => $str($row['mail_customer_body'] ?? null) ?? self::DEFAULT_CUSTOMER_BODY, 'owner_subject' => $str($row['mail_owner_subject'] ?? null) ?? self::DEFAULT_OWNER_SUBJECT, 'owner_body' => $str($row['mail_owner_body'] ?? null) ?? self::DEFAULT_OWNER_BODY, ]; } /** * Baut einen fertig konfigurierten, aber noch nicht abgeschickten PHPMailer aus den * uebergebenen SMTP-Feldern (host/port/username/password/encryption/from_email/from_name). */ public static function build(array $config): PHPMailer { $mail = new PHPMailer(true); $mail->isSMTP(); $mail->Host = $config['host']; $mail->Port = (int) $config['port']; $mail->SMTPAuth = true; $mail->Username = $config['username']; $mail->Password = $config['password']; $mail->SMTPSecure = ($config['encryption'] ?? '') !== 'none' ? $config['encryption'] : false; $mail->CharSet = 'UTF-8'; $mail->setFrom($config['from_email'], $config['from_name'] ?: $config['from_email']); return $mail; } /** * Ersetzt {{platzhalter}} in einer Vorlage. Unbekannte Platzhalter bleiben unveraendert * stehen (auffaelliger als sie einfach zu leeren, falls sich im Text ein Tippfehler einschleicht). */ public static function render(string $template, array $vars): string { $replacements = []; foreach ($vars as $key => $value) { $replacements['{{' . $key . '}}'] = $value ?? '-'; } return strtr($template, $replacements); } }