<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

include_once 'PHPMailer/src/PHPMailer.php';
include_once 'PHPMailer/src/SMTP.php';
require 'PHPMailer/gump.class.php';
include_once 'PHPMailer/src/Exception.php';

/* ----------------------------------------------------- */

$array_data = $_POST;
$isValid = GUMP::is_valid($array_data, array(
            'name' => 'required|valid_name',
            'email' => 'required|valid_email',
            'phone' =>  'required|numeric|exact_len,10',
            'message' => 'required',
        ));
if ($isValid === true) {
    $Get_Result = SendMailContacForm($array_data);

    wp_send_json($Get_Result);
    if ($Get_Result) {
        $result = array(
            'result' => 'success',
            'msg' => array('')
        );
        echo json_encode($result);
        exit();
    } else {
        $result = array(
            'result' => 'error',
            'msg' => array('Mail Not Send')
        );
        echo json_encode($result);
        exit();
    }
} else {
    $result = array(
        'result' => 'error',
        'msg' => $isValid
    );
    echo json_encode($result);
    exit();
}



/* ................Body Bild.................... */

function BuildBody($Array) {

    $body = "<h2>Your Book Request Just Came In From " . $_POST['trackme'] . "</h2>";
    $body .= "<p><b>Name:</b> " . $Array['name'] . '</p>';
    $body .= "<p><b>Email:</b> " . $Array['email'] . '</p>';
    $body .= "<p><b>Phone:</b> " . $Array['phone'] . '</p>';
        $body .= "<p><b>Message:</b> " . $Array['message'] . '</p>';




    return $body;
}

function SendMailContacForm($array_data) {
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
//    $Data_Array = $array_data;
    $mail = new PHPMailer(true);
    $mail->Subject = 'Schedule A Consultation ';
    $mail->Body = BuildBody($array_data);
    $mail->IsHTML(true); // send as HTML
    $mail->setFrom('no-reply@speakeasymarketinginc.com');
//    $mail->addAddress('muzzamilijaz@gmail.com');
    $mail->addAddress('myduiattorney113@gmail.com');

    $mail->addAddress('seolawyers2012@gmail.com');

    if ($mail->send()) {
        $result = array(
            'result' => 'success',
            'msg' => 'Your request has beeen sent successfully'
        );
        echo json_encode($result);
        exit;
    }
}



/* .............................Get Ip address Funcation............................... */

function getClientIP() {
    if (isset($_SERVER)) {
        if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
            return $_SERVER["HTTP_X_FORWARDED_FOR"];
        if (isset($_SERVER["HTTP_CLIENT_IP"]))
            return $_SERVER["HTTP_CLIENT_IP"];
        return $_SERVER["REMOTE_ADDR"];
    } if (getenv('HTTP_X_FORWARDED_FOR'))
        return getenv('HTTP_X_FORWARDED_FOR');
    if (getenv('HTTP_CLIENT_IP'))
        return getenv('HTTP_CLIENT_IP');
    return getenv('REMOTE_ADDR');
}
