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

"; $applicant_text_content = "Dear " . $name . ",\n\nThank you for applying for the position of " . $job['title'] . " at CosmicHire.\n\nWe have received your application and will be in touch shortly if your qualifications match our requirements.\n\nBest regards,\nThe CosmicHire Team"; MailService::sendMail($email, $applicant_subject, $applicant_html_content, $applicant_text_content); $success_message = 'Your application has been submitted successfully! A confirmation email has been sent to you.'; } else { $error_message = 'Your application was saved, but there was an error sending the notification email.'; // Optional: Log the detailed error: error_log($result['error']); } } catch (PDOException $e) { $error_message = 'There was a database error. Please try again later.'; // Optional: Log the detailed error: error_log($e->getMessage()); } } else { $error_message = 'There was an error uploading your resume. Please try again.'; } } } ?> Apply for <?php echo htmlspecialchars($job['title']); ?> - CosmicHire

Apply for

Cancel