'Method not allowed'], 405); } $input = json_decode(file_get_contents('php://input'), true); $email = trim($input['email'] ?? $_POST['email'] ?? ''); if (empty($email)) { jsonResponse(['error' => 'Email is required'], 400); } if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { jsonResponse(['error' => 'Please enter a valid email address'], 400); } // Check if already subscribed $existing = db()->fetch( "SELECT id FROM email_subscribers WHERE email = :email", ['email' => strtolower($email)] ); if ($existing) { jsonResponse(['error' => 'This email is already subscribed'], 400); } // Add subscriber try { db()->insert('email_subscribers', [ 'email' => strtolower($email), 'source' => 'website', 'is_active' => 1 ]); // Send welcome email $html = <<

Tom's Java Jive

Welcome to the Java Jive Family!

Thanks for subscribing to our newsletter. You'll be the first to know about:

As a thank you, enjoy 10% off your first order with code: WELCOME10

Shop Now

Tom's Java Jive | Premium Coffee

HTML; sendEmail($email, 'Welcome to Tom\'s Java Jive!', $html); jsonResponse(['success' => true, 'message' => 'Successfully subscribed!']); } catch (Exception $e) { jsonResponse(['error' => 'Subscription failed. Please try again.'], 500); }