143 lines
4.5 KiB
PHP
143 lines
4.5 KiB
PHP
<?php
|
|
// Fehlerausgabe komplett deaktivieren
|
|
error_reporting(0);
|
|
ini_set('display_errors', 0);
|
|
ini_set('log_errors', 1);
|
|
|
|
require_once dirname(__DIR__) . '/phpmailer/src/Exception.php';
|
|
require_once dirname(__DIR__) . '/phpmailer/src/PHPMailer.php';
|
|
require_once dirname(__DIR__) . '/phpmailer/src/SMTP.php';
|
|
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
|
use PHPMailer\PHPMailer\Exception;
|
|
|
|
class PHP_Email_Form {
|
|
public string $to;
|
|
public string $from_name;
|
|
public string $from_email;
|
|
public string $subject;
|
|
public string $message = '';
|
|
public array $smtp = [];
|
|
public bool $ajax = false;
|
|
public ?string $reply_to = null;
|
|
|
|
private string $error = '';
|
|
private array $error_messages = [
|
|
'invalid' => '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') . '<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;
|
|
}
|
|
|
|
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) . '<br>';
|
|
return;
|
|
}
|
|
|
|
if ($file['size'] > $max_size * 1024 * 1024) {
|
|
$this->error .= "Datei ist zu groß. Maximum: {$max_size}MB<br>";
|
|
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('<br>', "\n", $this->message));
|
|
|
|
$mail->send();
|
|
return 'OK';
|
|
|
|
} catch (Exception $e) {
|
|
return 'Mailer Error: ' . $mail->ErrorInfo;
|
|
}
|
|
}
|
|
}
|