'Please provide a valid email address.']); exit; } // Read existing subscriptions $subscriptions = json_decode(file_get_contents($subscriptionsFile), true); // Check if email already exists foreach ($subscriptions as $sub) { if ($sub['email'] === $email) { http_response_code(409); echo json_encode(['message' => 'This email is already subscribed.']); exit; } } // Add new subscription $newSubscription = [ 'email' => $email, 'notification_opt_in' => $notification_opt_in, 'subscribed_at' => date('Y-m-d H:i:s'), 'active' => true ]; $subscriptions[] = $newSubscription; // Save subscriptions if (file_put_contents($subscriptionsFile, json_encode($subscriptions, JSON_PRETTY_PRINT))) { http_response_code(200); echo json_encode(['message' => 'Successfully subscribed to our newsletter!']); } else { http_response_code(500); echo json_encode(['message' => 'Subscription failed. Please try again.']); } exit; ?>