<?php
session_start();
if($_SESSION['dbName']==""){
header("location:../index.php");
}

require 'PHPMailer-master/class.phpmailer.php';

include("config.inc.php");
//echo"<br>"."here01";
include("Functions_Purchase.php");
include("Forms_DataGrid1.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");


$vform   	 = $_GET['vform']; if ($vform == ""){$vform = $_POST["vform"];}
$sc	 = $_GET['sc']; if ($sc == ""){$sc = $_POST["sc"];}
$vfile	 = $_GET['vfile']; if ($vfile == ""){$vfile = $_POST["vfile"];}

$actionmode  = $_POST["actionmode"]; if ($actionmode == ""){$actionmode = $_GET["actionmode"];}
$vimagename = $_POST["vimagename"]; if ($vimagename == ""){$vimagename = $_GET["vimagename"];}

$toEmail 		= $_POST['toEmail'];
$emailSubject 	= $_POST['emailSubject'];
$sid = $_GET['SId'];

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

$foldername = "uploads/";

  if($sid !== '' ){
    if( $vform == "SalesJV") 
  $query = "Select client_Email, salesjv_reference from SalesJV
       inner join clients on clients.ledger_number = client_code
     where SalesJV_id = $sid";
     else if( $vform == "Sales") 
     $query = "Select client_Email, sale_reference from Sales
     inner join clients on clients.ledger_number = client_code
   where Sale_id = $sid";
     else 
     $query = "Select client_Email, delivery_reference from deliveries
     inner join clients on clients.ledger_number = client_code
   where delivery_id = $sid";
     $result = mysql_query($query, $connection);
     $row = mysql_fetch_array($result);
     $toEmail  = $row[0];
	 $ref = $row[1];
	 $emailSubject = "Invoice # ".$ref." ".$emailSubject;
	 
	//echo"<br>".  $toEmail;
}
if( $vform == "SalesJV") 
$url = $serverurl."form_SalesjvPreviewemail.php?SalesJVId=".$sid."&dbName=".$_SESSION['dbName']."&dbServer=".$config['dbServer']."&dbUser=".$config['dbUser']."&dbPass=".$config['dbPass']."&dbLabel=".$_SESSION['dbLabel'];
else if( $vform == "Sales") 
$url = $serverurl."form_SalesPreviewemail.php?SalesId=".$sid."&dbName=".$_SESSION['dbName']."&dbServer=".$config['dbServer']."&dbUser=".$config['dbUser']."&dbPass=".$config['dbPass']."&dbLabel=".$_SESSION['dbLabel'];
else
$url = $serverurl."form_deliveryPreviewemail.php?SalesId=".$sid."&dbName=".$_SESSION['dbName']."&dbServer=".$config['dbServer']."&dbUser=".$config['dbUser']."&dbPass=".$config['dbPass']."&dbLabel=".$_SESSION['dbLabel'];
// echo"<br>".$url;exit;
$cURL = curl_init($url);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true); 

$Body=curl_exec($cURL);

  // //  require 'PHPMailer-master/class.phpmailer.php';
	// // $fileType1 = $_FILES['filepdf']['type'];$fileName1 = $_FILES['filepdf']['name'];
	// // $tmpName1  = $_FILES['filepdf']['tmp_name'];$fileSize1 = $_FILES['filepdf']['size'];
	


	
	// // if (!is_dir($foldername))
	// // 		mkdir($foldername, 0700);
			
	
			
	// // $fp1 = fopen($tmpName1, 'r');
	// // $content1 = fread($fp1, $fileSize1);
	// // $content1 = addslashes($content1);
	// // fclose($fp1);
	// // if(!get_magic_quotes_gpc())
	// // 	$fileName1 = addslashes($fileName1);
	
	// // $dest1 = $foldername.'/'.$fileName1;
   
	// // $r1 = move_uploaded_file ($tmpName1, $dest1);	
	



	// //$to ="sally1abilmona2@gmail.com";
  //   $to = $toEmail;
  //   $mail = new PHPMailer(true);
  //   $mail->IsSMTP();
  // /*  $mail->Host =  $hostname; 
  //   $mail->SMTPAuth = true;

  //   $mail->Username =  $sender;      
  //   $mail->Password =  $pswd;    
  //   $mail->SMTPSecure = 'tls'; 
  //   $mail->CharSet = 'windows-1256';
  //   $mail->From = $sender;
  //   $mail->FromName = "DATACOM";
  //   $mail->WordWrap = 50;
  //   $mail->IsHTML(true);
	// $mail->Port = $row_setup[2]; 
	// */
	
  //   $mail->Host = "cpanel.cobranet.org";
  //   $mail->SMTPAuth = true;

  //   $mail->Username = "mailer@compusoft-ng.com";
  //   $mail->Password = "mailer@2019";
  //   $mail->SMTPSecure = "";
  //   $mail->CharSet = 'windows-1256';
  //   $mail->From = "mailer@compusoft-ng.com";
  //   $mail->FromName = "IPT ADMIN";
  //   $mail->WordWrap = 50;
  //   $mail->IsHTML(true);
  //   $mail->Subject = $emailSubject;
  //   $mail->AddAddress($to, " ");

  //   $mail->SMTPOptions = array(
  //       'ssl' => array(
  //           'verify_peer' => false,
  //           'verify_peer_name' => false,
  //           'allow_self_signed' => true
  //       )
  //   );

  //   $mail->Body    =  "testtt";
	// //$mail->addAttachment($dest1);
  //   //$mail->Port =26;
  //   $mail->Port = 25;
  //   $mail->SMTPKeepAlive = true;
  //   $mail->Mailer = "smtp";
  // //  echo"test";exit;
  $query_mailsetup="select hostname,mailagent,port,SenderEmail,ReceiverEmail,password, username from configurationemail";
  $result_mailsetup = mysql_query($query_mailsetup, $connection);
  $row_setup=mysql_fetch_row($result_mailsetup); 
  $hostname=$row_setup[0];
  $port=$row_setup[2];
  $sender=$row_setup[3];
  $pswd = $row_setup[5];
  $fromname = $row_setup[6];
  $recipients = $toEmail;
//   $recipients = 'sophiezehlawy@gmail.com';
  
//echo"<br>".  $recipients;
//exit;
  $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->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 = $emailSubject;
  //$mail->SMTPDebug = 0;
  $mail->Port =587; 
  //exit;
  $mail->AddAddress($recipients, '');
//var_dump($mail);exit;
    if (!$mail->send()) {
        $msg = "Mailer Error: " . $mail->ErrorInfo;
    } else {
        $msg = "Message sent!";
//        unlink($uploadfile);
    }
    if( $vform == "SalesJV") 
    header ("Location: ./Form_SalesJVSearch.php?message=".$msg);
    else if( $vform == "Sales") 
    header ("Location: ./Form_SalesSearch.php?message=".$msg);
     else
    header ("Location: ./Form_DeliverySearch.php?message=".$msg);
 


?>