<?php
session_start();
ob_start();
$emma='';
$pasw='';

include('../include/config.php');
//error_reporting(0);
//$iduser=$_SESSION['user_id'];
$date = new DateTime("now", new DateTimeZone('Africa/Nairobi') );
$timenow=$date->format('Y-m-d H:i:s');
$timenow2=$date->format('Y-m-d');
$month=$date->format('M');

function createRandomPassword() {
$chars = "003232303232023232023456789";
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;
while ($i <= 7) 
{
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}

$code=createRandomPassword();

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

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

$query=mysqli_query($con,"select * from companydata");
while($row=mysqli_fetch_array($query))
{
  $emma=htmlentities($row['cemail']);  
  $pasw=htmlentities($row['cpassword']); 
}


if(isset($_POST['em']))
{
 $em=addslashes($_POST['em']);
 $mail = new PHPMailer(true);

 $mail->IsSMTP();
 $mail->Host = 'smtp.gmail.com';
 $mail->SMTPAuth= true;
 $mail->Username =$emma;
 $mail->Password = $pasw;
 $mail->SMTPSecure = 'tls';
 $mail->Port = 587;
 
 $mail->setFrom($emma);
 $mail->addAddress($em);
 $mail->isHTML(true);
 $mail->Subject = "Bishop Kairo Education Centre - Login Credentials";
 //$mail->Subject = "Email Testing";
 $mail->Body = "<center><img src='https://bishopkairoeducationcentre.sc.ke/QB-Beta/images/logo.png' style='max-height:130px' class='img-circle elevation-2' /> </center> <br /> Following are your login credentials on for accounting system. <br />"."Username: ".$em.'<br />'." Password: ".$code."<br /> Kindly use the above login credentials to login and change your password which is convinient with you. <br />"."Click this link to visit the website: https://bishopkairoeducationcentre.sc.ke/QB-Beta/ "."<br /> <br />"."<strong>NB: </strong> <u><li><em>For any enquiry,</em> Contact the administration for assistant</li></ul>";

// $mail->Body = "Hae James";
 $pass=md5($code);
 $sql=mysqli_query($con,"UPDATE users SET Pass='$pass' where Email='$em'");  
$mail->send();
echo 'success';
}
?>