120) { back_with_error('Name is required and must be under 120 characters.'); } if (!filter_var($email, FILTER_VALIDATE_EMAIL) || strlen($email) > 190) { back_with_error('A valid email address is required.'); } if (strlen($message) < 10 || strlen($message) > 2000) { back_with_error('Message must be between 10 and 2000 characters.'); } if (strlen($company) > 160 || strlen($budget) > 80) { back_with_error('One of the optional fields is too long.'); } try { ensure_leads_table(); $token = bin2hex(random_bytes(16)); $ip = $_SERVER['REMOTE_ADDR'] ?? null; $agent = substr((string)($_SERVER['HTTP_USER_AGENT'] ?? ''), 0, 255); $stmt = db()->prepare('INSERT INTO leads (public_token, name, email, company, budget, message, source, ip_address, user_agent) VALUES (:token, :name, :email, :company, :budget, :message, :source, :ip, :agent)'); $stmt->bindValue(':token', $token); $stmt->bindValue(':name', $name); $stmt->bindValue(':email', $email); $stmt->bindValue(':company', $company !== '' ? $company : null); $stmt->bindValue(':budget', $budget !== '' ? $budget : null); $stmt->bindValue(':message', $message); $stmt->bindValue(':source', 'Landing page'); $stmt->bindValue(':ip', $ip); $stmt->bindValue(':agent', $agent); $stmt->execute(); $leadId = (int)db()->lastInsertId(); $safeName = e($name); $safeEmail = e($email); $safeMessage = nl2br(e($message)); $html = "

New landing page lead

Name: {$safeName}

Email: {$safeEmail}

Company: " . e($company ?: 'Not provided') . "

Budget: " . e($budget ?: 'Not sure') . "

Message:
{$safeMessage}

"; $text = "New landing page lead\nName: {$name}\nEmail: {$email}\nCompany: " . ($company ?: 'Not provided') . "\nBudget: " . ($budget ?: 'Not sure') . "\n\n{$message}"; $mailResult = MailService::sendMail(null, 'New landing page lead from ' . $name, $html, $text, ['reply_to' => $email]); if (!empty($mailResult['success'])) { $update = db()->prepare('UPDATE leads SET email_sent = 1 WHERE id = :id'); $update->bindValue(':id', $leadId, PDO::PARAM_INT); $update->execute(); } else { error_log('Lead notification email failed: ' . ($mailResult['error'] ?? 'unknown error')); } header('Location: thank-you.php?token=' . urlencode($token)); exit; } catch (Throwable $exception) { error_log('Lead submission failed: ' . $exception->getMessage()); back_with_error('We could not save your request right now. Please try again in a moment.'); }