<?php

require_once 'app/PHPMailerAutoload.php';

if($_SERVER["REQUEST_METHOD"] == "POST"){
    if($_POST['userStep']=='0')
    {
        sendEmail($_POST['email']);
    }
}

function sendEmail($email) 
{
    $newPass = generateRandomString();
    $pass= password_hash($newPass, PASSWORD_DEFAULT);
    
    $con  = new mysqli("localhost","tgate_murtaza","Murtaza03317117456","tgate_thai_gate");
      
        
    $mail = new PHPMailer;
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'mail.thai-gate.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'support@thai-gate.com';            // SMTP username
    $mail->Password = 'Murtaza03317117456';                  // SMTP password
    $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 465;                                    // TCP port to connect to
    $mail->setFrom('support@thai-gate.com', 'THAI Gate');
    $mail->addAddress($email);     // Add a recipient
    
    // $mail->addAttachment('http://smartklimat.org/api/payments/certificates/2XW89233CP555713Y.pdf', 'planteringsbevis.pdf');
    //$mail->addAttachment($data["txn_id"].'.pdf', 'planteringsbevis.pdf');                                  // Set email format to HTML
    $mail->Subject = 'Reset Password';
    
    $mail->Body    = "Your new Password is: ".$newPass;
    
    if($mail->send()) {
        $sql1 = "UPDATE users SET password = '$pass' WHERE email = '$email'";
      $result1= mysqli_query($con,$sql1);
      if($result1)
      {
          echo json_encode(array("status"=>"1"));
      }
        else
        {
          echo json_encode(array("status"=>"0"));
        }
    }
    else {
        echo '0';
    }
}

function generateRandomString($length = 5) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

?>