<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

session_start();

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'auth/mailer/src/PHPMailer.php';
require 'auth/mailer/src/SMTP.php';
require 'auth/mailer/src/Exception.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['email'])) {
    $email = $_POST['email'];

    // Generate 6-digit code
    $code = rand(100000, 999999);
    $_SESSION['verification_code'] = $code;
    $_SESSION['email_to_verify'] = $email;

    $mail = new PHPMailer(true);

    try {
       $mail->isSMTP();
$mail->Host = 'mail.gobolchain.com';  // ← CHANGE THIS
$mail->SMTPAuth = true;
$mail->Username = 'team@gobolchain.com';
$mail->Password = '@Jibala1997';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;

$mail->SMTPOptions = [
  'ssl' => [
    'verify_peer' => false,
    'verify_peer_name' => false,
    'allow_self_signed' => true
  ]
];

        // Email setup
        $mail->setFrom('team@gobolchain.com', 'Gobolchain');
        $mail->addAddress($email);
        $mail->isHTML(true);
        $mail->Subject = 'Your Gobolchain Verification Code';
        $mail->Body    = "Your verification code is: <strong>$code</strong>";

        $mail->send();
        echo "Verification code sent successfully";
    } catch (Exception $e) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    }
} else {
    echo "Invalid request.";
}
