<?php
session_start();

$email = $_POST['email'] ?? '';
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  echo json_encode(['status' => 'error', 'message' => 'Invalid email']);
  exit;
}

$otp = rand(100000, 999999);
$_SESSION['otp'] = $otp;
$_SESSION['otp_email'] = $email;

$subject = "Your OTP Code";
$message = "Your OTP is: $otp";
$headers = "From: noreply@yourdomain.com";

if (mail($email, $subject, $message, $headers)) {
  echo json_encode(['status' => 'success']);
} else {
  echo json_encode(['status' => 'error', 'message' => 'Mail sending failed']);
}
?>