<?php
header("Access-Control-Allow-Origin: *");
include 'conexion.php';
include "classes/class.phpmailer.php";

$getJson = file_get_contents('php://input');
$json = json_decode($getJson);
$userx = $json->userx;
$emailx = $json->emailx;


$updateAuth = $connect->query("UPDATE users SET auth_email=lpad(floor(0+ RAND()*10000),4,0) WHERE username='$userx'");
$getAuth = $connect->query("SELECT auth_email from users WHERE username='$userx'");
$result = array();
while ($fetchData = $getAuth->fetch_assoc()) {
	$result[] = $fetchData;
}
$authCode = $result[0]['auth_email'];

if ($authCode) {
	$mail = new PHPMailer;

	$mail->IsSMTP();

	$mail->SMTPSecure = 'ssl';

	//$mail->Host = "dharmaguna.id"; //hostname masing-masing provider email
	$mail->Host = "smtp.gmail.com";
	$mail->SMTPDebug = 2;
	$mail->Port = 465;
	// $mail->Port = 567;
	$mail->SMTPAuth = true;

	$mail->Timeout = 30; // timeout pengiriman (dalam detik)
	$mail->SMTPKeepAlive = true;


	$mail->Username = "gabrieleffendy@gmail.com"; //user email
	$mail->Password = "mpmuovlwwdytzdjz"; //password email
	$mail->SetFrom("gabrieleffendy@gmail.com", "Foxie"); //set email pengirim
	$mail->Subject = "Pemberitahuan Otentikasi Email Foxie"; //subyek email
	$mail->AddAddress($emailx, $userx); //tujuan email
	$mail->MsgHTML("Kode otentikasi email Anda : '$authCode'");

	if ($mail->Send()) {
		echo "Message has been sent";
	} else {
		echo 'Failed to sending message';
	}
}
