"; $headers[] = "Reply-To: {$name} <{$email}>"; $headers[] = "MIME-Version: 1.0"; $hasFile = isset($_FILES['file']) && $_FILES['file']['error'] === UPLOAD_ERR_OK; if ($hasFile) { $filename = basename($_FILES['file']['name']); $type = mime_content_type($_FILES['file']['tmp_name']) ?: 'application/octet-stream'; $allowed = ['application/pdf','image/png','image/jpeg']; if (!in_array($type, $allowed, true) || $_FILES['file']['size'] > 5*1024*1024) { http_response_code(400); exit("Ungültiger Anhang"); } $boundary = 'b'.md5(uniqid((string)mt_rand(), true)); $headers[] = "Content-Type: multipart/mixed; boundary=\"{$boundary}\""; $fileData = chunk_split(base64_encode((string)file_get_contents($_FILES['file']['tmp_name']))); $mailBody = "--{$boundary}\r\n"; $mailBody .= "Content-Type: text/plain; charset=UTF-8\r\n\r\n{$body}\r\n"; $mailBody .= "--{$boundary}\r\n"; $mailBody .= "Content-Type: {$type}; name=\"{$filename}\"\r\n"; $mailBody .= "Content-Transfer-Encoding: base64\r\n"; $mailBody .= "Content-Disposition: attachment; filename=\"{$filename}\"\r\n\r\n"; $mailBody .= "{$fileData}\r\n"; $mailBody .= "--{$boundary}--"; } else { $headers[] = "Content-Type: text/plain; charset=UTF-8"; $mailBody = $body; } // Senden $ok = mail( $to, '=?UTF-8?B?'.base64_encode('Kontakt: '.$subject).'?=', $mailBody, implode("\r\n", $headers) ); http_response_code($ok ? 200 : 500); echo $ok ? "OK" : "Mail send failed";