prepare("SELECT * FROM jobs WHERE id = ?"); $stmt->execute([$job_id]); $job = $stmt->fetch(); } // Redirect if job not found if (!$job) { header("Location: careers.php"); exit; } $success_message = ''; $error_message = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $name = trim($_POST['name'] ?? ''); $email = trim($_POST['email'] ?? ''); $resume = $_FILES['resume'] ?? null; if (empty($name) || empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL) || empty($resume) || $resume['error'] !== UPLOAD_ERR_OK) { $error_message = 'Please fill in all fields and upload a valid resume.'; } else { // Handle file upload $upload_dir = __DIR__ . '/uploads/resumes/'; $file_extension = pathinfo($resume['name'], PATHINFO_EXTENSION); $safe_filename = uniqid('resume_', true) . '.' . $file_extension; $upload_path = $upload_dir . $safe_filename; if (move_uploaded_file($resume['tmp_name'], $upload_path)) { try { // Save application to database $pdo = db(); $stmt = $pdo->prepare("INSERT INTO applications (job_id, name, email, resume_path) VALUES (?, ?, ?, ?)"); $stmt->execute([$job_id, $name, $email, $upload_path]); // Send email notification $to = getenv('MAIL_TO') ?: 'your-email@example.com'; // Fallback email $subject = "New Application for " . $job['title']; $html_content = "
A new application has been submitted for the position of " . htmlspecialchars($job['title']) . ".
" . "Applicant Name: " . htmlspecialchars($name) . "
" . "Applicant Email: " . htmlspecialchars($email) . "
" . "The resume has been saved to the server.
"; $text_content = "New Application for " . $job['title'] . "\n" . "Applicant Name: " . $name . "\n" . "Applicant Email: " . $email; $result = MailService::sendMail($to, $subject, $html_content, $text_content); if (!empty($result['success'])) { // Send confirmation email to applicant $applicant_subject = "Your Application for " . $job['title']; $applicant_html_content = "Dear " . htmlspecialchars($name) . ",
" . "Thank you for applying for the position of " . htmlspecialchars($job['title']) . " at CosmicHire.
" . "We have received your application and will be in touch shortly if your qualifications match our requirements.
" . "Best regards,
The CosmicHire Team