<?php
session_start();
use PHPMailer\PHPMailer\PHPMailer;

require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/SMTP.php';

// Form Processing Messages
$message_success = 'We have <strong>successfully</strong> received your Message and will get Back to you as soon as possible.';

// Add this only if you use reCaptcha with your Contact Forms
$recaptcha_secret = ''; // Your reCaptcha Secret

$mail = new PHPMailer();

// If you intend you use SMTP, add your SMTP Code after this Line


if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
	if( $_POST['contact']['email'] != '' ) {
        $gender = isset( $_POST['contact']['gender'] ) ? $_POST['contact']['gender'] : '';
		$first_name = isset( $_POST['contact']['first_name'] ) ? $_POST['contact']['first_name'] : '';
		$last_name = isset( $_POST['contact']['last_name'] ) ? $_POST['contact']['last_name'] : '';
		$mobile = isset( $_POST['contact']['mobile'] ) ? $_POST['contact']['mobile'] : '';
		$email = isset( $_POST['contact']['email'] ) ? $_POST['contact']['email'] : '';
		$message = isset( $_POST['contact']['message'] ) ? $_POST['contact']['message'] : '';
		$code = isset( $_POST['contact']['code'] ) ? $_POST['contact']['code'] : '';

		$subject = 'New Message From Contact Form';

		$captcha = $_SESSION['captcha']['code'];

		if( $captcha == $code ) {
		    require '../../../config.smtp.php';
		    $mail->IsSMTP();
            $mail->Port = $smtp['Port'];
		    $mail->SMTPAuth = $smtp['SMTPAuth'];
		    $mail->SMTPSecure = $smtp['SMTPSecure'];
		    $mail->SMTPAutoTLS = $smtp['SMTPAutoTLS'];
		    $mail->SMTPDebug = $smtp['SMTPDebug'];
		    $mail->Host = $smtp['Host'];
		    $mail->Username = $smtp['Username'];
		    $mail->Password = $smtp['Password'];
            $mail->CharSet = $smtp['CharSet'];
            $mail->Encoding = $smtp['Encoding'];
			$mail->SetFrom( $email , $first_name . ', ' . $last_name . ' ' . $gender );
			$mail->AddReplyTo( $email , $first_name . ', ' . $last_name . ' ' . $gender );
			foreach( $toemails as $toemail ) {
				$mail->AddAddress( $toemail['email'] , $toemail['name'] );
			}
			$mail->Subject = $subject;

			$name = "Name: " . $gender;
			$name .= isset($first_name) ? ($first_name . ', ') : '';
			$name .= isset($last_name) ? $last_name : '';
			$email = isset($email) ? "Email: $email<br><br>" : '';
			$phone = isset($phone) ? "Phone: $phone<br><br>" : '';
			$message = isset($message) ? "Message: $message<br><br>" : '';

			$referrer = $_SERVER['HTTP_REFERER'] ? '<br><br><br>This Form was submitted from: ' . $_SERVER['HTTP_REFERER'] : '';

			$body = "$name $email $phone $message $referrer";

			// Runs only when File Field is present in the Contact Form
//			if ( isset( $_FILES['template-contactform-file'] ) && $_FILES['template-contactform-file']['error'] == UPLOAD_ERR_OK ) {
//				$mail->IsHTML(true);
//				$mail->AddAttachment( $_FILES['template-contactform-file']['tmp_name'], $_FILES['template-contactform-file']['name'] );
//			}
//
//			// Runs only when reCaptcha is present in the Contact Form
//			if( isset( $_POST['g-recaptcha-response'] ) ) {
//				$recaptcha_response = $_POST['g-recaptcha-response'];
//				$response = file_get_contents( "https://www.google.com/recaptcha/api/siteverify?secret=" . $recaptcha_secret . "&response=" . $recaptcha_response );
//
//				$g_response = json_decode( $response );
//
//				if ( $g_response->success !== true ) {
//					echo '{ "alert": "error", "message": "Captcha not Validated! Please Try Again." }';
//					die;
//				}
//			}

			// Uncomment the following Lines of Code if you want to Force reCaptcha Validation

			// if( !isset( $_POST['g-recaptcha-response'] ) ) {
			// 	echo '{ "alert": "error", "message": "Captcha not Submitted! Please Try Again." }';
			// 	die;
			// }

			$mail->MsgHTML( $body );
			$sendEmail = $mail->Send();
			if( $sendEmail == true ):
				echo '{ "alert": "success", "message": "' . $message_success . '" }';
			else:
				echo '{ "alert": "error", "message": "Email <strong>could not</strong> be sent due to some Unexpected Error. Please Try Again later.<br /><br /><strong>Reason:</strong><br />' . $mail->ErrorInfo . '" }';
			endif;
		} else {
			echo '{ "alert": "error", "message": "Captcha <strong>Invalid</strong>.!" }';
		}
	} else {
		echo '{ "alert": "error", "message": "Please <strong>Fill up</strong> all the Fields and Try Again." }';
	}
} else {
	echo '{ "alert": "error", "message": "An <strong>unexpected error</strong> occured. Please Try Again later." }';
}

?>