<?php
include "../database.php";
require_once '../../plugins/sendgrid/config.php';
require "../../plugins/sendgrid/sendgrid-php.php";
session_start();
$code        = rand(1000, 9999);
$date        = date("Y-m-d H:i:s");
$currentDate = strtotime($date);
$futureDate  = $currentDate + (60 * 5);
$formatDate  = date("Y-m-d H:i:s", $futureDate);
$sql_insert  = "INSERT INTO verification_codes VALUES (NULL,'" . $_SESSION["email"] . "','Password reset'," . $code . ",'" . $formatDate . "')";
if ($con->query($sql_insert) === true) {
 //Invio mail creatore
 $email = new \SendGrid\Mail\Mail();
 $email->setFrom("info@n-hub.com", "NH-HR");
 $email->addTo("" . $_SESSION["email"] . "", "" . $_SESSION["username"] . "");
 $email->SetTemplateId("d-4cc102622d9e45f780395130e8e44196");
 //Here is the Place holder values you need to replace.
 $email->addDynamicTemplateData("NHHR_verification_code", "$code");
 $email->addDynamicTemplateData("NHHR_code_expiration", "" . date('d/m/Y H:i', strtotime($formatDate)) . "");
 $sendgrid = new \SendGrid(SENDGRID_API_KEY);
 try {
  $response = $sendgrid->send($email);
  echo "ok";
 } catch (Exception $e) {
  echo 'Caught exception: ' . $e->getMessage() . "\n";
 }
} else {
 echo "error";
}
