'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); $message = htmlspecialchars($content, ENT_QUOTES, 'UTF-8') . '
'; if ($length_check !== null && strlen($content) < $length_check) { $this->error .= $label . ' ' . $this->error_messages['short'] . '
'; return; } $this->message .= !empty($label) ? "$label: $message" : $message; } 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'] ); } // Content $mail->isHTML(true); $mail->Subject = $this->subject; $mail->Body = $this->message; $mail->AltBody = strip_tags(str_replace('
', "\n", $this->message)); $mail->send(); return 'OK'; } catch (Exception $e) { return 'Mailer Error: ' . $mail->ErrorInfo; } } }