Feature: Professionelles HTML-Mail-Template für Kontaktformular

- Modernes responsives Design mit Gradient-Header
- Tabellarische Darstellung der Formulardaten
- Inline CSS für maximale E-Mail-Client-Kompatibilität
- Plain-Text Fallback (AltBody) beibehalten
- Zeilenumbrüche werden korrekt in HTML konvertiert
- XSS-Schutz und Sicherheit beibehalten
- HKW Branding im Footer
This commit is contained in:
2026-02-03 10:35:53 +01:00
parent e8abebca66
commit 7e8d862aea

View File

@@ -57,14 +57,33 @@ class PHP_Email_Form {
public function add_message(string $content, string $label = '', ?int $length_check = null): void {
$content = strip_tags($content);
$message = htmlspecialchars($content, ENT_QUOTES, 'UTF-8') . '<br>';
if ($length_check !== null && strlen($content) < $length_check) {
$this->error .= $label . ' ' . $this->error_messages['short'] . '<br>';
return;
}
$this->message .= !empty($label) ? "<strong>$label:</strong> $message" : $message;
$escaped_content = htmlspecialchars($content, ENT_QUOTES, 'UTF-8');
$escaped_content = nl2br($escaped_content); // Zeilenumbrüche in <br> konvertieren
if (!empty($label)) {
$this->message .= "
<tr>
<td style='padding: 12px 15px; border-bottom: 1px solid #e0e0e0; background: #f8f9fa; font-weight: 600; color: #2c3e50; width: 150px;'>
$label:
</td>
<td style='padding: 12px 15px; border-bottom: 1px solid #e0e0e0; color: #34495e;'>
$escaped_content
</td>
</tr>";
} else {
$this->message .= "
<tr>
<td colspan='2' style='padding: 12px 15px; border-bottom: 1px solid #e0e0e0; color: #34495e;'>
$escaped_content
</td>
</tr>";
}
}
public function add_attachment($field_name, $max_size = 20, $allowed_types = ['pdf', 'doc', 'docx', 'jpg', 'jpeg', 'png', 'gif']): void {
@@ -127,11 +146,14 @@ class PHP_Email_Form {
);
}
// HTML Template für professionelle E-Mail
$html_body = $this->buildHtmlTemplate();
// Content
$mail->isHTML(true);
$mail->Subject = $this->subject;
$mail->Body = $this->message;
$mail->AltBody = strip_tags(str_replace('<br>', "\n", $this->message));
$mail->Body = $html_body;
$mail->AltBody = strip_tags(str_replace(['<br>', '</tr>'], ["\n", "\n"], $this->message));
$mail->send();
return 'OK';
@@ -140,4 +162,63 @@ class PHP_Email_Form {
return 'Mailer Error: ' . $mail->ErrorInfo;
}
}
private function buildHtmlTemplate(): string {
$current_year = date('Y');
return <<<HTML
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{$this->subject}</title>
</head>
<body style="margin: 0; padding: 0; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; background-color: #f4f4f4;">
<table role="presentation" style="width: 100%; border-collapse: collapse; background-color: #f4f4f4;">
<tr>
<td style="padding: 40px 20px;">
<table role="presentation" style="width: 100%; max-width: 600px; margin: 0 auto; background-color: #ffffff; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1);">
<!-- Header -->
<tr>
<td style="padding: 30px 30px 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 8px 8px 0 0; text-align: center;">
<h1 style="margin: 0; color: #ffffff; font-size: 24px; font-weight: 600;">
Neue Kontaktanfrage
</h1>
<p style="margin: 10px 0 0; color: #e0e7ff; font-size: 14px;">
HKW Anwälte Webformular
</p>
</td>
</tr>
<!-- Content -->
<tr>
<td style="padding: 30px;">
<p style="margin: 0 0 20px; color: #555; font-size: 15px; line-height: 1.6;">
Sie haben eine neue Nachricht über das Kontaktformular erhalten:
</p>
<table role="presentation" style="width: 100%; border-collapse: collapse; border: 1px solid #e0e0e0; border-radius: 6px; overflow: hidden;">
{$this->message}
</table>
</td>
</tr>
<!-- Footer -->
<tr>
<td style="padding: 20px 30px 30px; background-color: #f8f9fa; border-radius: 0 0 8px 8px; text-align: center;">
<p style="margin: 0; color: #888; font-size: 13px; line-height: 1.5;">
Diese E-Mail wurde automatisch generiert.<br>
Bitte antworten Sie nicht auf diese E-Mail.<br>
<strong style="color: #555;">HKW Anwälte</strong> &copy; {$current_year}
</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
HTML;
}
}