<?php



if($_POST)

{
require('constant.php');

    

  

   $user_phone     = filter_var($_POST["phone"], FILTER_SANITIZE_STRING);

			

    if(empty($user_phone)) {

		$empty[] = "<b>Mobile Number</b>";		

	}else if(!empty($user_phone)){

		if(!preg_match("/^\d{12}$/",$user_phone)){

			$empty[] = "<b>Valid Mobile Number</b>";

			$success = 0;

		}

	}

	

		

	

	if(!empty($empty)) {

		$output = json_encode(array('type'=>'error', 'text' => implode(", ",$empty) . ' Required!'));

        die($output);

	}
	

	if($success !== 0){
		
	$url = "http://dummy.restapiexample.com/api/v1/create";
	$data = array(
        'phn'=>$user_phone,
       
);
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data)
    )
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
	
}



	if($result)	{
	$to = 'nandini.yatam@vectramind.com';

	//$to = 'murthy.dirisala@vectramind.com';

    $subject = 'UnSubscribe';

    $from = $user_phone;

	$headers  = 'MIME-Version: 1.0' . "\r\n";

    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

	$message = '<html><body>';

	$message .= '<h1 style="color:#f40;">UnSubscribe!</h1>';

	$message .= '<p style="color:#080;font-size:18px;">Phone: '.$result.' </p>';

	$message .= '<p style="color:#080;font-size:18px;">Phone: '.$user_phone.' </p>';

	$message .= '</body></html>';

	



	if(mail($to, $subject, $message, $headers,"-f User@vectramind.com")){

	    $output = json_encode(array('type'=>'message', 'text' => 'Unsubscribed Successfully'));

	    die($output);


	} else {

	    $output = json_encode(array('type'=>'error', 'text' => 'Unable to send email, please contact'.SENDER_EMAIL));

	    die($output);

	}

	

	}
if (empty($result)){
	$empty[] = "<b>Something went wrong</b>";	
	
	if(!empty($empty)) {

		$output = json_encode(array('type'=>'error', 'text' => ' Something went wrong!'));

        die($output);

	}

	
}

}

?>