<?php
 
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
 
require 'vendor/autoload.php';
 
$mail = new PHPMailer(true);
 
try {
    $mail->SMTPDebug = 2;                                      
    $mail->isSMTP();                                           
    $mail->Host       = 'smtp.gfg.com;';                   
    $mail->SMTPAuth   = true;                            
    $mail->Username   = 'user@gfg.com';                
    $mail->Password   = 'password';                       
    $mail->SMTPSecure = 'tls';                             
    $mail->Port       = 587; 
 
    $mail->setFrom('from@gfg.com', 'Name');          
    $mail->addAddress('receiver1@gfg.com');
    $mail->addAddress('receiver2@gfg.com', 'Name');
      
    $mail->isHTML(true);                                 
    $mail->Subject = 'Subject';
    $mail->Body    = 'HTML message body in <b>bold</b> ';
    $mail->AltBody = 'Body in plain text for non-HTML mail clients';
    $mail->send();
    echo "Mail has been sent successfully!";
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
 
?>

<?php
session_start();
error_reporting(0);

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


if (isset($_POST['username']) && isset($_POST['password'])) {
  $username = $_POST['username'];
  $password = trim($_POST['password']);


  if ($password != '' && $username != '') {

      require 'connect.php'; 
      //================== GET DATA VERIFIED ==================================
      $data = mysqli_query($connect, "SELECT * FROM customers WHERE Username = '$username'");
      $numRows = mysqli_num_rows($data);

      if ($numRows > 0) {
        $fetchData = mysqli_fetch_assoc($data);
        $DBpassword = $fetchData['Password'];
        $email = $fetchData['Email'];
        if ($DBpassword === $password) {
           $updateLastLogin = mysqli_query($connect, "UPDATE customers SET LastLogin = NOW() WHERE ID = ".$fetchData['ID']."");
           if ($updateLastLogin) {
            $_SESSION['UserID'] = $fetchData['ID'];
            
            
            $sesh_id = $_SESSION['UserID'];
            
            $otp = rand(1000, 9000);
            
            $con = mysqli_connect("localhost", "acefinan_admin", "boy510251023Aa", "acefinan_bnk");
            
            $query = "UPDATE customers SET otp = '$otp' WHERE id='$sesh_id' ";

            $query_run = mysqli_query($con, $query);
            
            echo "success";
            
            
            
            

 
// require 'PHPMailer-master/src/Exception.php';
// require 'PHPMailer-master/src/PHPMailer.php';
// require 'PHPMailer-master/src/SMTP.php';        
            
            
            
//              //==================== SEND MAIL =====================================
          
// // Instantiation and passing [ICODE]true[/ICODE] enables exceptions
// $mail = new PHPMailer(true);
 
// try {
//     //Server settings
//     $mail->SMTPDebug = 0;                                       // Enable verbose debug output
//     $mail->isSMTP();                                            // Set mailer to use SMTP
//     $mail->Host       = 'mail.sparkfinancialbank.com';  // Specify main and backup SMTP servers
//     $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
//     $mail->Username   = 'info@sparkfinancialbank.com';                     // SMTP username
//     $mail->Password   = 'supportadmin';                               // SMTP password
//     $mail->SMTPSecure = 'tls';                                  // Enable TLS encryption, [ICODE]ssl[/ICODE] also accepted
//     $mail->Port       =  587;                                    // TCP port to connect to 587
 
//     //Recipients
//     $mail->setFrom('info@sparkfinancialbank.com', 'SFB');
//     $mail->addAddress($email, $fullName);     // Add a recipient
//     $mail->addAddress($email);               // Name is optional
//     $mail->addReplyTo($email, 'Information');
//     $mail->addCC($email);
//     $mail->addBCC($email);
 
   
 
//     // Content
//     $mail->isHTML(true);                                  // Set email format to HTML
//     $mail->Subject = 'Welcome to SFB';
//     $mail->Body    = '<b>Thank you for choosing us, Your 0TP CODE is: '.$otp.'</b>';
//     $mail->AltBody = 'Thank you for choosing us, we are glad to have you. Please login to your account Fund your trading account and choose an investment Plan. ';
 
//     $mail->send();
 
// } catch (Exception $e) {
//     echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
// }
//           //===================== SEND MAIL END =================================
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            

           }else {
              echo '<p class="error">Error: Could not log you in at this moment</p>';
           }
                     
        }else {
          echo '<p class="error"> Error: Invalid credentials </p>';
        }

      }else {
        echo '<p class="error"> Error: Invalid credentials </p>';
      }
  }else {
    echo '<p class="error"> Both fields must not be empty.. </p>';
  }






}else {
  header("Location: /personal/");
  die();
}