connect_error) { throw new Exception("חיבור למסד הנתונים נכשל: " . $conn->connect_error); } // 2) grab & trim everything $name = trim($_POST['name'] ?? ''); $email = trim($_POST['email'] ?? ''); $tel = trim($_POST['tel'] ?? ''); $subject_raw = trim($_POST['subject'] ?? ''); $message = trim($_POST['message'] ?? ''); $car_id = trim($_POST['car_id'] ?? ''); $form_page_title = trim($_POST['form_page_title'] ?? ''); $form_page_url = trim($_POST['form_page_url'] ?? ''); // 3) validation $missing = []; if ($name === '') $missing[] = 'name'; if ($tel === '') $missing[] = 'tel'; if ($message === '') $missing[] = 'message'; if (!empty($missing)) { // DEBUG: return what we received error_log("Validation failed, missing: " . implode(',', $missing) . " | \$_POST=" . json_encode($_POST, JSON_UNESCAPED_UNICODE)); throw new Exception("נא למלא את כל השדות הנדרשים"); } // 4) build a subject for DB / email if ($car_id !== '' && ctype_digit($car_id)) { $subject_db = "פנייה לרכב #{$car_id}"; } else { $subject_db = "פנייה כללית מדף יצירת קשר"; } if ($subject_raw !== '') { $subject_db .= " | {$subject_raw}"; } // 5) build the DB message blob $db_message = $message; if ($car_id) $db_message .= "\n\nמזהה רכב: $car_id"; if ($form_page_title) $db_message .= "\nכותרת הדף: $form_page_title"; if ($form_page_url) $db_message .= "\nכתובת הדף: $form_page_url"; // 6) insert into contact_us (adjust columns if yours differ) $stmt = $conn->prepare( "INSERT INTO contact_us (name, tel, subject, message) VALUES (?, ?, ?, ? )" ); if (!$stmt) { throw new Exception("שגיאה בהכנת השאילתה: " . $conn->error); } $stmt->bind_param('ssss', $name, $tel, $subject_db, $db_message); if (!$stmt->execute()) { throw new Exception("שגיאה בשמירת הנתונים: " . $stmt->error); } $stmt->close(); // 7) compose the mail body $mail_body = ""; if ($car_id !== '' && ctype_digit($car_id)) { // look up title $cstmt = $conn->prepare("SELECT title FROM cars_sale_inventory WHERE id = ?"); if ($cstmt) { $cstmt->bind_param("i", $car_id); $cstmt->execute(); $cres = $cstmt->get_result(); if ($crow = $cres->fetch_assoc()) { $mail_body .= "📌 פנייה לגבי רכב #{$car_id}: {$crow['title']}\n"; $mail_body .= "🔗 https://{$_SERVER['HTTP_HOST']}/car/{$car_id}\n\n"; } $cstmt->close(); } } else { $mail_body .= "📨 פנייה כללית מדף יצירת קשר\n\n"; } $mail_body .= "👤 שם: {$name}\n"; $mail_body .= "✉️ אימייל: {$email}\n"; $mail_body .= "📞 טלפון: {$tel}\n\n"; if ($subject_raw) { $mail_body .= "📝 נושא: {$subject_raw}\n\n"; } $mail_body .= "💬 הודעה:\n{$message}\n\n"; if ($form_page_title) { $mail_body .= "🏷 כותרת הדף: {$form_page_title}\n"; } if ($form_page_url) { $mail_body .= "🔗 כתובת הדף: {$form_page_url}\n"; } // 8) send the email $to = $settings['contact_email'] ?? 'tan.luxurymotors@gmail.com'; $mail_subject = "פנייה חדשה באתר: " . ($subject_raw ?: 'ללא נושא'); $headers = "From: {$name} <{$email}>\r\n" . "Reply-To: {$email}\r\n" . "Content-Type: text/plain; charset=UTF-8\r\n"; @mail($to, $mail_subject, $mail_body, $headers); // 9) done echo json_encode(['status'=>'success'], JSON_UNESCAPED_UNICODE); exit; } catch (Exception $e) { http_response_code(400); echo json_encode([ 'status' => 'error', 'error' => $e->getMessage() ], JSON_UNESCAPED_UNICODE); exit; }