Zaktualizuj funkcję wysyłania e-maili, aby obsługiwała wiele załączników oraz dodaj walidację plików

This commit is contained in:
2026-02-08 22:07:22 +01:00
parent cf05060d01
commit 5f1dbd9e19
2 changed files with 381 additions and 52 deletions

View File

@@ -3,7 +3,7 @@ session_start();
require_once 'phpmailer/class.phpmailer.php';
require_once 'phpmailer/class.smtp.php';
function send_email( $email, $reply, $subject, $text, $attachment = null )
function send_email( $email, $reply, $subject, $text, $attachments = array() )
{
$mail = new PHPMailer;
$mail -> IsSMTP();
@@ -27,17 +27,88 @@ function send_email( $email, $reply, $subject, $text, $attachment = null )
$mail -> isHTML( true );
$mail -> Subject = $subject;
$mail -> Body = $text;
if ($attachment && isset($attachment['tmp_name']) && file_exists($attachment['tmp_name'])) {
$mail->addAttachment($attachment['tmp_name'], $attachment['name']);
// Obsługa wielu załączników
if (is_array($attachments) && count($attachments) > 0) {
foreach ($attachments as $attachment) {
if (isset($attachment['tmp_name']) && file_exists($attachment['tmp_name'])) {
$mail->addAttachment($attachment['tmp_name'], $attachment['name']);
}
}
}
return $mail -> send();
}
function validate_file($file) {
$maxSize = 10 * 1024 * 1024; // 10MB
$allowedExtensions = array('jpg', 'jpeg', 'png', 'pdf', 'doc', 'docx', 'xls', 'xlsx');
$allowedMimeTypes = array(
'image/jpeg',
'image/jpg',
'image/png',
'application/pdf',
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
);
// Sprawdź czy plik istnieje
if (!isset($file['tmp_name']) || !file_exists($file['tmp_name'])) {
return array('valid' => false, 'error' => 'Plik nie istnieje');
}
// Sprawdź rozmiar
if ($file['size'] > $maxSize) {
return array('valid' => false, 'error' => 'Plik jest za duży (max 10MB)');
}
// Sprawdź rozszerzenie
$fileExtension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
if (!in_array($fileExtension, $allowedExtensions)) {
return array('valid' => false, 'error' => 'Niedozwolone rozszerzenie pliku');
}
// Sprawdź MIME type
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimeType = finfo_file($finfo, $file['tmp_name']);
finfo_close($finfo);
if (!in_array($mimeType, $allowedMimeTypes)) {
return array('valid' => false, 'error' => 'Niedozwolony typ pliku');
}
return array('valid' => true);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$configData = json_decode($_POST['configData'], true);
$file = isset($_FILES['attachment']) ? $_FILES['attachment'] : null;
// Pobierz wszystkie załączniki
$attachments = array();
$attachments_count = isset($_POST['attachments_count']) ? intval($_POST['attachments_count']) : 0;
// Walidacja liczby załączników (max 10)
if ($attachments_count > 10) {
echo json_encode(['status' => 'error', 'message' => 'Maksymalnie 10 załączników']);
exit();
}
for ($i = 0; $i < $attachments_count; $i++) {
$fileKey = 'attachment_' . $i;
if (isset($_FILES[$fileKey])) {
$file = $_FILES[$fileKey];
// Walidacja pliku
$validation = validate_file($file);
if (!$validation['valid']) {
echo json_encode(['status' => 'error', 'message' => 'Błąd walidacji pliku: ' . $validation['error']]);
exit();
}
$attachments[] = $file;
}
}
$to = 'kontakt@ostal.pl';
$subject = 'ostal.pl - Konfigurator';
@@ -94,7 +165,18 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$message .= '</ul>';
}
if (send_email($to, $configData['step_4']['email'], $subject, $message, $file)) {
// Dodaj informację o załącznikach
if (count($attachments) > 0) {
$message .= '<p>Załączniki (' . count($attachments) . '):</p>';
$message .= '<ul>';
foreach ($attachments as $attachment) {
$fileSize = round($attachment['size'] / 1024, 2); // KB
$message .= '<li>' . htmlspecialchars($attachment['name']) . ' (' . $fileSize . ' KB)</li>';
}
$message .= '</ul>';
}
if (send_email($to, $configData['step_4']['email'], $subject, $message, $attachments)) {
$_SESSION["configurator_sended"] = true;
echo json_encode(['status' => 'ok', 'message' => 'Wiadmość wysłana']);
} else {