<?php  

  session_start();
  error_reporting(0);
   include("../db.php");
	 include("../all_query.php");
		
		
		
	$host="localhost";
	  $username="finmarnf";
	  $password="Finman!@123";
	  $database="finmarnf_bhawani_crm";
 	   
	   
	 
	 
	  $bulk_emailQ = mysqli_query($conn,"select *from `bulk_email` where is_sent = 0  limit 1 ");
	  
	  while($bulk_emailData = mysqli_fetch_assoc($bulk_emailQ))
	  {
	 
	      $bulk_email_id = $bulk_emailData['id'];
	 
 
		  $account_holder_id_data = $bulk_emailData['account_holder_id_data'];
		  $template_id = $bulk_emailData['template_id'];
		  $text_message = $bulk_emailData['text_message'];
		  
		  $attach_statement = '';
		  
		 
		 
		 
		  $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;
		  }
		  
		  
		  
		  
		  
		  $account_holder_id_dataArray = explode(',',$account_holder_id_data);
		  
		  $i = 0;
		  
		  foreach($account_holder_id_dataArray as $account_holder_id)
		  {
		  
		  
		  $i++;
		  
		  
		 $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'];
		}
		 
		  
		 
		 
		 
		 $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");
			
			 
			
			$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();
	}
		  
		  
		  
		  
		  
		  // Sleep after 10 iterates
		  
		  
		    if ($i > 0 && $i % 2 == 0) 
			{
				sleep(8);
				
			}
	
	
		  
		  
		  
		  
		  }
		  
		  
		  
		  
		  
		  
		  
		  
		  
		  mysqli_query($conn,"update bulk_email  set is_sent = 1 where id = $bulk_email_id ");
		  
		  
		   
		   
		   
	  }
		   
		   
		   
		   
		   
		   
		   
		   
 


?>