<?php 	
       use Dompdf\Dompdf;
       use PHPMailer\PHPMailer\PHPMailer;
       use PHPMailer\PHPMailer\Exception;	   
       require __DIR__.'/vendor/autoload.php';
      session_start();
      require_once '../Server/connection.php';

      if($_POST){
	
      $file_id = $_POST['file_id'];
	  $message = addslashes($_POST['message']);
	  $mailfrom = 'kreenlogisticstz@gmail.com';
			
		//use template		
		ob_start();
		require("client_File.php");
		$html = ob_get_contents();
        ob_end_clean();	
		
		//$token = $orderNo.rand(10,9999);
        //$link = "<a href='localhost/iws/confirmInvoice.php?key=".$order_id."&token=".$token."'> Click This link to Confirm invoice</a>";		

		
		$dompdf = new DOMPDF();
        $dompdf->load_html($html);
        $dompdf->set_paper("letter","portrait");	
        $dompdf->render();
		$filename = 'ClientFile.pdf';	
		$pdfoutput = $dompdf->output();
		//$dompdf->stream("FomuYaMaombiYaMkopo.pdf");
		
		
     
 $mail = new PHPMailer;
 $mail->IsSMTP();        //Sets Mailer to send message using SMTP
 $mail->Host = 'smtp.gmail.com';  //Sets the SMTP hosts of your Email hosting, this for Godaddy
 $mail->Port = '465';        //Sets the default SMTP server port
 $mail->SMTPAuth = true;       //Sets SMTP authentication. Utilizes the Username and Password variables
 $mail->Username = 'jacalminja929@gmail.com';     //Sets SMTP username
 $mail->Password = 'kknipxvlrhpyopga';     //Sets SMTP password
 $mail->SMTPSecure = 'ssl';       //Sets connection prefix. Options are "", "ssl" or "tls"
 $mail->From = $mailfrom;   //Sets the From email address for the message
 $mail->FromName = 'Kreen Logistics';   //Sets the From name of the message
 $mail->AddAddress($mailto, $firstname);  //Adds a "To" address
 $mail->WordWrap = 50;       //Sets word wrapping on the body of the message to a given number of characters
 $mail->IsHTML(true);       //Sets message type to HTML  
 
  $empQuery8 = "select * from client_docs where file_id='$file_id'";
  $empRecords8 = mysqli_query($conn, $empQuery8);
  $count8 = mysqli_num_rows($empRecords8);
  
     if($count8>0){
  $empQuery88 = "select * from client_docs where file_id='$file_id'";
  $empRecords88 = mysqli_query($conn, $empQuery88);
  while ($row88 = mysqli_fetch_assoc($empRecords88)){
	  $fileName88 = $row88['fileName'];
	   $doc88 = '"files/'.$fileName88.'"';
       $mail->AddStringAttachment($doc88);
             }
	         }
 
 //$mail->AddStringAttachment($pdfoutput,'Application/pdf',$filename,false);
 $mail->AddStringAttachment($pdfoutput,$filename);
 
 $mail->Subject = 'Client File And Attachment';   //Sets the Subject of the message
 //$mail->Body = 'Please See Attachment of your pdf invoice.';    //An HTML or plain text message body
 $mail->Body  = $message;
 
 
 
 if($mail->Send())        //Send an Email. Return true on success or false on error
 {
	 
  //$result22=$conn->query("UPDATE orders SET dueDate='$dueDate', order_status=1 WHERE order_id = '$order_id'");

  //if($result22){
	  
  
  $_SESSION['success_message'] = "File And Attachment have been sent to Client email successfully";
  header("Location: clientFiles.php");
  exit();
  
     //}
  
 }else{
	echo 'Error'; 
 }
 


} // /if $_POST
// echo json_encode($valid);