<?php  

  session_start();
  error_reporting(0);
   include("../db.php");
	 include("../all_query.php");
		

	 if(isset($_SESSION['loan_crm_username']) && isset($_SESSION['loan_crm_password']))
	{
		  
		  
		  $account_holder_id = $_REQUEST['account_holder_id'];
		  $template_id = $_REQUEST['template_id'];
		  $text_message = $_REQUEST['text_message'];
		  
		  $attach_statement = '';
		  
		  
		  if(isset($_REQUEST['attach_statement']))
			{
			 $attach_statement = 'on';
			}
		  
		  if($account_holder_id == '0')
		  {
			  
				  $INSERT_DATA  = array();
				  
				  $INSERT_DATA['template_id'] = $template_id;
				  $INSERT_DATA['text_message'] =  $text_message;
				  $INSERT_DATA['attach_statement'] = $attach_statement;
				  $INSERT_DATA['account_holder_id_data'] = implode(',',$_SESSION['all_row_ids']);
				   
				   
				 insertTable($conn,'bulk_email',$INSERT_DATA,'add_submit');
				
			  
			  exit;
		  }
		  
		  
		  
		  
		  
		  
		   
		  $content = '';
		  $subject = '';
		  
          if($template_id!='0')
		  {
 		 
		 $templateQ = mysqli_query($conn,"select *from `compaign_templates` where id=$template_id ");
		 $templateData = mysqli_fetch_assoc($templateQ);
		 $content = $templateData['content'];
		 $subject = $templateData['subject'];
		  }
		  else
		  {
			  $content = $text_message;
		  }
		  
		  
		  
		  
		  
		  
		  
		 $customerQ = mysqli_query($conn,"select *from `customer` where account_holder_id = '$account_holder_id' ");
	     $customerData = mysqli_fetch_assoc($customerQ);
		 
		 
		 
		 $leadQ=mysqli_query($conn,"select *from leads  where  account_holder_id = '$account_holder_id' order by id desc ");
		 
		 $total_loan = mysqli_num_rows($leadQ);
		 
		 $total_loan_amount = 0;
		 $total_tos = 0;
		 
		 
		while($leadData = mysqli_fetch_assoc($leadQ))
		{
			$total_loan_amount = $total_loan_amount + $leadData['loan_amount'];
			$total_tos = $total_tos + $leadData['tos'];
		}
		 
		  
		 
		 $file3 = '../images/'.'Statement_'.str_replace(' ','_',$customerData['name']).'.pdf';

		  
		 
		 
		 $leadQ=mysqli_query($conn,"select *from leads  where  account_holder_id = $account_holder_id order by id desc limit 1 ");
		 $leadData = mysqli_fetch_assoc($leadQ);
		 $lead_data = $leadData['lead_data'];
		 $data = json_decode(stripslashes($lead_data)); 
		 
	  
	  
		  $content =    str_replace('&gt;','>',$content);
		  $content =    str_replace('&lt;','<',$content);
		  $content =    str_replace('&nbsp;',' ',$content);
		  $content =    str_replace('{','<?= ',$content);
		  $content =    str_replace('}',' ?>',$content);
		  $content =    str_replace('^','\'',$content);
		  
		  
		  
		  
		  $fp = fopen("mail_content_html.php", "w");
		  fwrite($fp, $content);
		  fclose($fp);
		 
	 
	   
	        ob_start();

		   $content_html = include 'mail_content_html.php';
		   $content_html = ob_get_clean();
		   
		   
		    //echo $content_html;
		    // exit;
		  
 
		   
		   
		   	 $recipients = array(
		array(
			'email' => $customerData['email'],
			'name' => $customerData['name']
		) );
	try {
		  require_once 'SendCleanMTA_PHP-master/SendClean.php';
		 $SendClean= new SendClean('25833713', 'pFdBjYPk2cs0YXlK7y02', 'us1-mta1.sendclean.net');
		$smtp_user_name = "smtp78596013";
		foreach ($recipients as $recipient) {
			$message = array();
			$message["html"] = $content_html;
			$message["subject"] = $subject;
			$message["from_email"] = "info@finmanservices.in";
			$message["to"] = array(
				array("email" => $recipient['email'], "name" => $recipient['name'], "type" => "to")
			);
			$message["headers"] = array("Reply-To" => "info@finmanservices.in", "X-Unique-Id" => "Id");
			
			
			if(isset($_REQUEST['attach_statement']))
			{
				 $fp =    @fopen($file3,"rb");
                  $data =  @fread($fp,filesize($file3));

					@fclose($fp);
					$data = chunk_split(base64_encode($data));
							
			 $message["attachments"] = array(array("type" => "application/octet-stream", "name" => basename($file3), "content" => $data));
			}
			
			
			$result = $SendClean->messages->sendMail($smtp_user_name, $message);
			print_r($result);
			/*
			  Array
			  (
			  [status] => "success"
			  [message] => "message have been Queued ... "
			  )
			 */
		}
	} catch (SendClean_Error $e) {
		// SendCleanerrors are thrown as exceptions
		echo "A SendCleanerror occurred: " . get_class($e) . " - " . $e->getMessage();
	}
		  
		  
		  
		   
		   
		   
		   
		   
		   
		   
		   
		   
		 
		 
	}


?>