'Etwas ist schiefgelaufen. Bitte versuchen Sie es später noch einmal.',
'invalid_email' => 'E-Mail-Adresse scheint ungültig zu sein.',
'short' => 'ist zu kurz oder leer.',
'file_error' => 'Fehler beim Hochladen der Datei.'
];
private $attachments = [];
private function preSend(): bool {
if (empty($this->to)) {
$this->error = 'Empfänger-E-Mail fehlt.';
return false;
}
if (empty($this->from_email)) {
$this->error = 'Absender-E-Mail fehlt.';
return false;
}
if (empty($this->subject)) {
$this->error = 'Betreff fehlt.';
return false;
}
if (empty($this->message)) {
$this->error = 'Nachricht fehlt.';
return false;
}
return true;
}
public function add_message(string $content, string $label = '', ?int $length_check = null): void {
$content = strip_tags($content);
if ($length_check !== null && strlen($content) < $length_check) {
$this->error .= $label . ' ' . $this->error_messages['short'] . '
';
return;
}
$escaped_content = htmlspecialchars($content, ENT_QUOTES, 'UTF-8');
$escaped_content = nl2br($escaped_content); // Zeilenumbrüche in
konvertieren
if (!empty($label)) {
$this->message .= "
|
$label:
|
$escaped_content
|
";
} else {
$this->message .= "
|
$escaped_content
|
";
}
}
public function add_attachment($field_name, $max_size = 20, $allowed_types = ['pdf', 'doc', 'docx', 'jpg', 'jpeg', 'png', 'gif']): void {
if (!isset($_FILES[$field_name]) || $_FILES[$field_name]['error'] !== UPLOAD_ERR_OK) {
return;
}
$file = $_FILES[$field_name];
$ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
if (!in_array($ext, $allowed_types)) {
$this->error .= 'Ungültiger Dateityp. Erlaubt sind: ' . implode(', ', $allowed_types) . '
';
return;
}
if ($file['size'] > $max_size * 1024 * 1024) {
$this->error .= "Datei ist zu groß. Maximum: {$max_size}MB
";
return;
}
$this->attachments[] = [
'path' => $file['tmp_name'],
'name' => $file['name'],
'type' => $file['type']
];
}
public function send(): string {
try {
if (!$this->preSend()) {
return $this->error;
}
$mail = new PHPMailer(true);
// Server settings
$mail->isSMTP();
$mail->Host = $this->smtp['host'];
$mail->SMTPAuth = $this->smtp['auth'] ?? true;
$mail->Username = $this->smtp['username'];
$mail->Password = $this->smtp['password'];
$mail->SMTPSecure = $this->smtp['secure'] ?? PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = $this->smtp['port'];
$mail->CharSet = 'UTF-8';
// Recipients
$mail->setFrom($this->from_email, $this->from_name);
$mail->addAddress($this->to);
if (!empty($this->reply_to)) {
$mail->addReplyTo($this->reply_to);
}
// Attachments
foreach ($this->attachments as $attachment) {
$mail->addAttachment(
$attachment['path'],
$attachment['name'],
'base64',
$attachment['type']
);
}
// HTML Template für professionelle E-Mail
$html_body = $this->buildHtmlTemplate();
// Content
$mail->isHTML(true);
$mail->Subject = $this->subject;
$mail->Body = $html_body;
$mail->AltBody = strip_tags(str_replace(['
', ''], ["\n", "\n"], $this->message));
$mail->send();
return 'OK';
} catch (Exception $e) {
return 'Mailer Error: ' . $mail->ErrorInfo;
}
}
private function buildHtmlTemplate(): string {
$current_year = date('Y');
return <<
{$this->subject}
Neue Kontaktanfrage
HKW Anwälte Webformular
|
|
Sie haben eine neue Nachricht über das Kontaktformular erhalten:
|
|
Diese E-Mail wurde automatisch generiert.
Bitte antworten Sie nicht auf diese E-Mail.
HKW Anwälte © {$current_year}
|
|
HTML;
}
}