<?php
include 'connect.php';
header('Content-Type: application/json; charset=utf-8');
header("Access-Control-Allow-Origin: *");
$json = file_get_contents('php://input');
$obj = json_decode( $json );
$usernum =  $obj->{'usernumber'} ;
$countryCode =  $obj->{'countrycode'} ;
//$fromforgot =  $obj->{'fromforgot'} ;

$usernumber = '+'.$countryCode.$usernum;

require_once 'Twilio/autoload.php';
use Twilio\Rest\Client;

$twilio_number = "+14793232115";//"+447723365161";
$account_sid = "AC3182fb6256eae9aa859c3f112ee29a1d";//"ACdfcacc9f322707d47cabc84276f86a84";
$auth_token = "d64c62578e977983f6f0be51a0596410";//"23cb0b5d43067f760c9b5a9991c01150";

$account_sid = $account_sid;
$auth_token = $auth_token;
//$usernumber = '+918013247774';
$usrcel = $usernumber;
$r = mt_rand(100000, 999999);
$otp = $r;
$sms = $lang_resources->YOUR_OTP_NUMBER.": ".$otp;
$sms = 'your otp is'.": ".$otp;
$msg = $sms;

$client = new Client($account_sid, $auth_token);
$otpsendflag = 0;

try {
   $message = $client->messages->create(
   '+'.$usrcel,
   array(
       'from' => $twilio_number,
       'body' => $msg
   )
);
$otpsendflag = 1;
}
catch (exception $e) {
   //code to handle the exception
  $otpsendflag = 2;
}

if(isset($obj->{'fromforgot'}) && $obj->{'fromforgot'} == 1){
   $email = $obj->{'usermail'};
      $msg.='<!DOCTYPE html>
      <html>
      <head>
         <title>Change status</title>
         <meta charset="utf-8">
      </head>
      <body>
         <div style="width: 550px;margin: 0 auto;background-color: #fff;text-align: center;font-family: Helvetica, Arial, sans-serif;color: #333;">
            <img alt="logo" width="400px" style="margin: 10px 0;" src="https://res.cloudinary.com/dmw8kjmxe/image/upload/v1762586992/logotype_crjqcr.png"/>
            <hr style="border: 0;border-bottom: 1px solid #ddd;">
            <div style="text-align: center;margin: 20px 0;font-weight: lighter;line-height: 22px;">';

            $msg.='<p style="margin: 0px 0px 10px 0px; font-size:20px; color:#EF1962; margin:30px 0px 30px 0px">Your OTP is: <strong>'.$otp.'</strong></p>';


            $msg.='<div style="padding: 30px 0;background-color: #333;">

            </div>
         </div>
      </body>
      </html>';

      include_once('phpmailer/PHPMailerAutoload.php');

      $mail = new PHPMailer();
      $mail->isSMTP();                                            // Send using SMTP
      $mail->Host       = 'smtp.mailgun.org';                    // Set the SMTP server to send through
      $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
      $mail->Username   = 'no-reply@mail.orderdish.com';                     // SMTP username
      $mail->Password   = 'e2r7qqF@enMs*f6L!@7Z';                               // SMTP password
      $mail->SMTPSecure = "ssl";         // Enable TLS encryption; PHPMailer::ENCRYPTION_SMTPS encouraged
      $mail->Port       = 465;                                    // TCP port to connect to, use 465 for PHPMailer::ENCRYPTION_SMTPS
      $mail->setFrom('transactions@ordering-mail.com');
      $mail->addAddress($email);     // Add a recipient
      $mail->isHTML(true);                                  // Set email format to HTML
      $mail->Subject = 'OTP';
      $mail->Body    = $msg;
      $msggg = $mail->send();
      $response['mail'] = $msggg;


}

$response['otpsendflag'] = $otpsendflag;
$response['sms'] = $msg;
$response['code'] = $otp;
$response['error'] = false;
echo json_encode($response);

?>
