<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");

if(isset($_POST['email']))
{

include 'config.php';
date_default_timezone_set("Africa/Lagos");


$email = $_POST['email'];
$email=stripslashes($email);
$email=mysqli_escape_string($con, $email);


$sql=$con->query("SELECT * FROM users  WHERE  email = '$email'") or die("Error2 : ". mysqli_error($con));
$count = mysqli_num_rows($sql);

if($count >= 1)
{
$rows=mysqli_fetch_array($sql);
$id = $rows['id'];
$email = $rows['email'];
$user_id = $rows['user_id'];

 $code = rand (100000, 999999);    
 $code_exp = strtotime("+10 minutes"); 


$sqlx = $con->query("UPDATE users SET ver_code = '$code', code_expiry = '$code_exp'  WHERE email ='$email'") or die("Error2 : ". mysqli_error($con));


$subject = "Your Verification Code!";
$message = '<!DOCTYPE HTML>     
    <html>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
</head>
<body>
<div style="background-color:#eee; border:solid; border-width:thin; border-color:#EEE; position: relative; font-size:13px; padding-top:2em; padding:1em; font-family:Verdana, Geneva, sans-serif">
<div style="background-color:white; width: 80%; margin:auto; dborder:solid; dborder-width:thin; bdorder-color:#EEE; position: relative; font-size:13px; dtext-align: center; line-height: 20px; padding-top:2em; padding:1.5em; font-family:Verdana, Geneva, sans-serif">
<div style="width: 100%; text-align: center;">
</div>
<p>You just created an account on Cordis.</p>
<p>Your verification code is '.$code.'</p>
</div>
</div>

</body>
</html>';




$mail = new PHPMailer();

require 'mail_head.php';

$mail->setFrom('dev@diimdevs.com', 'Cordis');
$mail->addAddress($email);
//$mail->addAddress("gbite.aina@gmail.com");
$mail->addBcc("diimtech@gmail.com" );
$mail->Subject = $subject;
$mail->msgHTML($message);


/*$mail1->WordWrap = 50;                                 // set word wrap to 50 characters
//$mail->AddAttachment("/var/tmp/file.tar.gz");         // add attachments
//$mail->AddAttachment("/tmp/image.jpg", "new.jpg");    // optional name
$mail1->IsHTML(true);                                  // set email format to HTML */

$mail->Send();

$suc = "Code sent succesfully"; 
$status = true; 

}
else
{
$suc = "User not found"; 
$status = false; 

}
$con->close();

header('Content-Type: application/json; charset=utf-8');
$output = array('status'=>$status, 'success'=>$suc, 'email'=>$email, 'user_id'=>$user_id);
echo json_encode($output); //output JSON data
}
             
?>