<?php

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

require_once __DIR__ . '/vendor/phpmailer/src/Exception.php';
require_once __DIR__ . '/vendor/phpmailer/src/PHPMailer.php';
require_once __DIR__ . '/vendor/phpmailer/src/SMTP.php';

// passing true in constructor enables exceptions in PHPMailer
$mail = new PHPMailer(true);

$name = isset($_POST["enq_name"]) ? trim($_POST["enq_name"]) : '';
$country_code = isset($_POST["country_code"]) ? trim($_POST["country_code"]) : '';
$phone = isset($_POST["phone"]) ? trim($_POST["phone"]) : '';
$email = isset($_POST["enq_email"]) ? trim($_POST["enq_email"]) : '';
$message = isset($_POST["enq_comment"]) ? trim($_POST["enq_comment"]) : '';
$flag = isset($_POST["flag"]) ? trim($_POST["flag"]) : '';
$honeypot = htmlspecialchars(trim($_POST["company_name"] ?? ''));
$Project = "24 Wallstreet";
$PropertyFor = 'Buy';
$Property = 'Flat';
$Propertytype = 'BHK';
$Location = 'PCMC';



try {
    if ($honeypot) {
        header('Location: index.html?r=1');
        error_log("Alert IP: " . $_SERVER['REMOTE_ADDR'] . " is spammer."); // Log mailer errors
        exit("Spam detected!");
    }
    if ($name != '' && $phone != '') {
        if (isset($_POST['cFlag']) && !empty($_POST['cFlag'])) {
            die();
        }
        if (isset($_POST['fullMobileNo']) && empty($_POST['fullMobileNo'])) {
            die();
        }
        if (preg_match('/www\.|http:|https:/i', $message) <= 0 && strpos($message, 'google.com') === false && preg_match('/www\.|http:|https:/i', $name) <= 0 && strpos($name, 'google.com') === false) {
            // Server settings
            // $mail->SMTPDebug = SMTP::DEBUG_SERVER; // for detailed debug output
            $mail->isSMTP();
            $mail->Host = 'mail.smtp2go.com';
            $mail->SMTPAuth = true;
            $mail->SMTPSecure = 'tls';
            $mail->Port = 587;

            $mail->Username = 'dmxdigital'; // YOUR gmail email
            $mail->Password = 'okQLYLleqoNP'; // YOUR gmail password

            if ($flag == 0 && $message == "") {
                $message = "Pre-Registered for best offers.";
            }

            // Sender and recipient settings
            $mail->setFrom('leads@dmxdigital.in', '24 Wallstreet');
            $mail->addAddress("rajesht1900@gmail.com", "Enquiry Email");
            $mail->addAddress("pathan.parvez@yahoo.com", "24 Wallstreet");

            $mail->addReplyTo('rajesht1900@gmail.com', '24 Wallstreet Pune'); // to set the reply to





            // Setting the email content
            $mail->IsHTML(true);
            $mail->Subject = "Email Enquiry from - 24 Wallstreet";
            $mail->Body = '<b>See Below The Enquiry - </b><br><b>Name:</b>' . $name . ",<br><b>Phone:</b>" . $phone . ",<br><b>Email:</b>" . $email . ",<br><b>Message:</b>" . $message;
            $mail->AltBody = 'Plain text message body for non-HTML email client. Gmail SMTP email body.';

            $mail->send();



            //Code for Contact Information CMR Push - Start
            $url = "https://proprise.leadpluss.com/Services/api/TenantLeads/Submit";

            $curl = curl_init($url);
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_POST, true);;
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

            $headers = array();
            $headers["Content-Type"] = "application/x-www-form-urlencoded";

            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

            $data = "vendor_key=6212FFE0-77D0-4CE9-8569-EB0581CC43DB&PropertyFor=$PropertyFor&Property=$Property&PropertyType=$Propertytype&Project=$Project&Location=$Location&Phone=$phone&FirstName=$name&EmailId=$email&Message=$message";


            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

            //for debug only!
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

            $resp = curl_exec($curl);
            curl_close($curl);


            header('Location: index.html?r=1');
            exit;
        } else {
            header('Location: index.html?r=11');
            exit;
        }
    } else {
        header('Location: index.html?r=0');
        exit;
    }
} catch (Exception $e) {
    header('Location: index.html?r=0');
    exit;
    // echo "Error in sending email. Mailer Error: {$mail->ErrorInfo}";
}
