<?php
session_start();
//error_reporting(E_ALL & ~ E_NOTICE);
$rollnum = $_POST["rollnum"];

$sfile_to_read = fopen('student-emails/rec-mails.csv', 'r');
$studentemail = "harish";

if($sfile_to_read !== FALSE){
    while(($sdata = fgetcsv($sfile_to_read, 100, ',')) !== FALSE){
        if (in_array($rollnum, $sdata)){
            $studentemail = $sdata[1];
        }
    }
    fclose($sfile_to_read);
}
class Controller
{
    function __construct() {
        $this->processEmailVerification();
    }
    function processEmailVerification()
    {
        global $studentemail;
        switch ($_POST["action"]) {
            case "get_otp":
                $email = $studentemail;
                $otp = rand(100000, 999999); //generates random otp
                $_SESSION['session_otp'] = $otp;
                // Compose a simple HTML email message
                // $htmlContent = '<html>
                // <head><p>One time password</p></head>
                // <body><span><p class="p3">'.$otp.'</p></span></body>
                // </html>';
                $message = "Your One Time Password for See the Results " . $otp;
                $headers = "MIME-Version: 1.0" . "\r\n"; 
                $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; 
                $sub = "Email verification from Raghu Engineering College results page ";
                $headers = "From : REC Results <results@raghuengineeringcollege.in>";
                try{
                    $retval = mail($email,$sub,$headers,$message);
                    if($retval)
                    {
                        require_once('otp-verificationnew.php');
                        json_encode(array("otp" => $otp, "emailmessage" => "OTP is sent to Your Email id". $email ));
                    }
                }
                catch(Exception $e)
                {
                    die('Error: '.$e->getMessage());
                }
 
                break;
                
            case "verify_otp":
                $otppost = $_POST['otp'];
                
                if ($otppost == $_SESSION['session_otp']) 
                {
                   json_encode(array("type"=>"success", "message"=>"Your Email is verified!", "otp" => $otppost, "sessionotp" => $_SESSION['session_otp']));
                   require_once('resultnew.php');
                   unset($_SESSION['session_otp']);
                } 
                else {
                   echo "OTP is not valid";
                }
                break;
        }
    }
}
$mail->Mailer = "smtp";  
$mail->Host = "smtp.secureserver.net"; 
$mail->Port = 465;  
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "test1@raghuenggcollege.in"; // SMTP username  
$mail->Password = "Akv@123#123"; // SMTP password 
$controller = new Controller();
?>