<?php

require 'PHPMailer-master/class.phpmailer.php';
session_start();
if($_SESSION['dbName']==""){
    header("location:../index.php");
} 

include("config.inc.php");
include("function.inc.php");
include("function_Clientsexceeded.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");

$querysetup= " select  base1,base2,DecimalBase  from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}
$querysetup="select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2,maxperpage,company_name from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}
mysql_query("SET NAMES 'utf8'"); 
mysql_query('SET CHARACTER SET utf8'); 		
mysql_set_charset('utf8', $connection);


$actionmode = $_GET['actionmode']; if ($actionmode == '') { $actionmode = $_POST['actionmode']; }
$fromclient = $_GET['fromclient'];
//$toclient = $_GET['toclient'];

$serverurl = $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
$temp = explode("/contract/", $serverurl);
$serverurl = $temp[0]."/contract/";

// $comp=explode('_',$_SESSION['dbName']);
// if($comp[0]=='azar'){
//     $company='Azar Chemical Industries Limited';
// }

$year= date ("Y");
$fromdate="01/01/".$year;
$todate=date("d/m/Y");
// jp
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if($actionmode == 'sendEmail'){
   // echo"aaaa";exit;

    foreach($_POST as $key => $value){
        $a[$key]=$value;
        if(strpos($key, "txtchecked") !== false){
            $code=substr($key,10);
            

           echo $query="select ifnull(client_email,'') as ClientEMAIL,ledger_name from clients where ledger_number = '$code' ";
            $result=mysql_query($query,$connection) or die(mysql_error());
            $row = mysql_fetch_array($result);
           //echo"asd ".$row[0];exit;
            if($row[0] !=''){
               // echo "<BR>ACCESSED<BR>";
            //     $query_days="select ifnull(DATEDIFF(now(),(select max(ES_SendDate) from EmailsSent where ES_ledger='$code' )),'100000')";
            //    $result_days=mysql_query($query_days, $connection);
            //     $row_days=mysql_fetch_array($result_days); 
                
            //     if($row_days[0]>30 || $row_days=='' ){
                
                $Subject = "Client Contract";			
                

                    $HTML="Dear Valued Customer,<br><br>

                    Do not respond as this is an auto generated email.<br>
                    
                    Kindly be informed that .......<br>

                    
                    Thank you for your business and looking forward to working with you in the future.<br><br>
                    
                    Sincerely yours,<br>
                    
                    ";
                   //echo "<br>".$HTML; 
                   try{
                    $mail=sendmail($company_name,$Subject,$HTML,$row[0].";" ,$connection); 
                   // $queryins="Insert into  EmailsSent ( ES_ledger, ES_SendDate) values ($code , now())";
                   // $result=mysql_query($queryins,$connection) or die(mysql_error());
                    }catch (Exception $e) {
                        echo 'Caught exception: ',  $e->getMessage(), "\n";
                    }
                            
                
            } 
                
            }
           //echo "<HR>"; 
        }           
        //echo"<br>";
    
    header ("Location: ./Form_ClientsContract.php");
} 




function sendmail($from,$Subject,$Body,$emails,$connection){
        $recipients="";
        
     
       //// Configuration
       $query_mailsetup="select hostname,mailagent,port,SenderEmail,ReceiverEmail,password, username from configurationemail";
       $result_mailsetup = mysql_query($query_mailsetup, $connection);
       
       $row_setup=mysql_fetch_array($result_mailsetup); 
       $hostname=$row_setup[0];
       $port=$row_setup[2];
       $sender=$row_setup[3];
       $pswd = $row_setup[5];
       $fromname = $row_setup[6];
       
    
    
    
    
    
    
    
    
    
       $mail = new PHPMailer;
    //    $mail->IsSMTP();                                      // Set mailer to use SMTP
    //    $mail->Host = $hostname; 							// Specify main and backup server
    //    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    //    $mail->Username = $sender;                            // SMTP username
    //    $mail->Password = $pswd;                           // SMTP password
        $mail->isSMTP();
        $mail->Host = 'smtp.mailtrap.io';
        $mail->SMTPAuth = true;
        $mail->Username = '047a5cbc02155f'; //paste one generated by Mailtrap
        $mail->Password = '65f6fcc26fe3d3'; //paste one generated by Mailtrap
        // $mail->SMTPSecure = 'tls';
        $mail->Port = 2525;
       //$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted
       $mail->From = $sender;
       $mail->FromName = $fromname;	
       $mail->WordWrap = 50;                                 // Set word wrap to 50 characters
       $mail->Body    =  $Body;
       $mail->IsHTML(true);                                  // Set email format to HTML
       $mail->Subject = $Subject;
       //$mail->SMTPDebug = 0;
       $mail->Port =587; 
       //exit;
       
       
       echo "<pre>";
       var_dump($emails);
       echo "</pre>";
           $emailtechs = explode(';',$emails);
        //    foreach($emailtechs as $emailtech){
        //    $mail->AddAddress($emailtech, '');  // Add a recipient
        //    $recipients = $recipients." $emailtech,";
        //    }
        
           $recipients = $emailtechs[0] ;
           $mail->AddAddress($recipients, '');
       
       if(!$mail->Send()) {
         $error = $mail->ErrorInfo;
        
         return false;	
       }else       
       return true;
    }
?>