<?php

    $user_email=htmlentities($_POST['email']);
    $user_name=htmlentities($_POST['name']);
    $user_phone=htmlentities($_POST['phone']);
    $user_form_title="Contact Provident East Lalbagh Discovery";
    $user_source= htmlentities($_POST['source']);
     $gclid_field= htmlentities($_POST['gclid_field']);
     //  Send lead to Propacity
    $url = "https://api.propacity.in/api/v1/webhooks/integration/d6ee7a58-20fe-4c7f-9b52-45858ff7d9b8/insertLead";              // paste url here

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

    $headers = array(
    "Content-Type: application/json",
    );
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

    $data = [
        'email' => $user_email,
        'name' => $user_name,
        'mobile' => $user_phone
    ];


    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));


    $resp = curl_exec($curl);
   

    curl_close($curl);

    // End of send lead to Propacity
						
						 function getVisIpAddr() { 
      
								if (!empty($_SERVER['HTTP_CLIENT_IP'])) { 
									return $_SERVER['HTTP_CLIENT_IP']; 
								} 
								else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { 
									return $_SERVER['HTTP_X_FORWARDED_FOR']; 
								} 
								else { 
									return $_SERVER['REMOTE_ADDR']; 
								} 
							}   
							// Store the IP address 
							$vis_ip = getVisIPAddr(); 
							// Use JSON encoded string and converts 
							// it into a PHP variable 
							$ipdat = @json_decode(file_get_contents( 
							  "http://www.geoplugin.net/json.gp?ip=" . $vis_ip)); 
						   $country_name=$ipdat->geoplugin_countryName;   
						   $handle = curl_init();
						   $url = "http://www.geoplugin.net/extras/postalcode.gp?format=json&lat=".$ipdat->geoplugin_latitude."&lon=".$ipdat->geoplugin_longitude;
						   // Set the url
						   curl_setopt($handle, CURLOPT_URL, $url);
						   // Set the result output to be a string.
						   curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); 
						   $output = curl_exec($handle); 
							curl_close($handle);
							$output=json_decode($output);
							$city=$output->geoplugin_place;
							$pincode=$output->geoplugin_postCode;
							$ip_location=$city.", ".$country_name.", ".$pincode;
							 $ip = $_SERVER['REMOTE_ADDR'];

    $user_body_text='<table class="tg" style="margin: 0 auto;width: 60%;background-color: #fff;">
					<tr>
						<th class="tg-031e" style="background-color: #efefef; padding:20px;">
							<img  src="" width="600px" height="200px">
						</th>
					</tr>
					<tr>
						<td class="tg-yw4l" style="border: 0;padding: 13px!important;">
							<p>Hi there,</p>
							<p> We have received one new registration information on this account from Provident East Lalbagh  Discovery website. </p>
						</td>
					</tr>
					<tr>
						<td class="tg-yw4l" style="border: 0;padding: 13px!important;">
							<center>
							<table>
								<tbody>
								 <tr>
										<th style="text-align: left;display: table;">gclid_field:-</th>
										<td>'.$gclid_field.'</td>
									</tr>
								    <tr>
										<th style="text-align: left;display: table;">Source:-</th>
										<td>'.$user_source.'</td>
									</tr>
									<tr>
										<th style="text-align: left;display: table;">Name:-</th>
										<td>'.$user_name.'</td>
									</tr>
									<tr>
										<th style="text-align: left;display: table;">Email:-</th>
										<td>'.$user_email.'</td>
									</tr>
									<tr>
										<th style="text-align: left;display: table;">Contact Number:-</th>
										<td>'.$user_phone.'</td>
									</tr>
									<tr>
										<th style="text-align: left;display: table;">IP Address:-</th>
										<td>'.$ip.'</td>
									</tr>
									<tr>
										<th style="text-align: left;display: table;">IP Location:-</th>
										<td>'.$ip_location.'</td>
									</tr>
								</tbody>
							</table>
							</center>
						</td>
					</tr>
					<tr>
						<td class="tg-yw4l" style="border: 0;padding: 13px!important;">
							<p>Need help? You can reply to this message directly or contact us by other means.</p>
							<p>Our customer service team is available weekdays from 8 to 5 Pacific Time.</p>
						</td>
					</tr>
					
					<tr style="border-bottom: 1px solid!important;padding: 10px;">
						<td class="tg-yw4l" style="border: 0;">
							<p style="margin-left:13px;">Thanks,</p>
							<p style="margin-left:13px;">Team Provident East Lalbagh  Discovery</p>
						</td>
					</tr>
					
				</table>';

    require 'mailer/autoload.php';
    function sendMail($to, $from, $fromName, $subject, $bodyOfEmail) {
    $mail = new PHPMailer;
    $mail->isSMTP(true);
    $mail->Host = 'mail.sales.ind.in';
    $mail->Port = '587';
        $mail->SMTPSecure = 'tls';
        $mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    ));
        $mail->SMTPAuth = true;
 $mail->Username = 'server@sales.ind.in';
        $mail->Password = 'Nitin@123#';
        $mail->setFrom($from, $fromName);
        foreach ($to as $value){
         $mail->addAddress($value);   
        }
        $mail->Subject = $subject;
        $mail->isHTML(true);
        $mail->Body = $bodyOfEmail;
        $mail->AltBody = "Hello, my friend! This message uses plain text !";
        if($mail->send()){
            return true;
        }else{
            return false;
        }
        
    }
if(!empty($user_email) && !empty($user_name) && !empty($user_phone)){
   //$receiver=array('rkg12310@gmail.com');

      $receiver=array('vistaarrealestate@gmail.com','leads@sales.ind.in','varun_aggarwal29@outlook.com',);
    if(sendMail($receiver, "vbuzzleads@gmail.com" ,"leads", $user_form_title, $user_body_text)){
        
        ?>
        <script>window.location.href = 'thankyou.php'; </script>
        <?php 
    }else{
     echo "Unable to sent mail at that moment! Please try again later.";
    }
}else{
     echo "Please fill the all details!.";
}
?>