<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
//ini_set('allow_url_fopen', 1);

// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('Asia/Kolkata');
$data=date("Y-m-dH-i-s"); 
require_once 'config.php';
//require_once 'dompdf/autoload.inc.php';
use Dompdf\Dompdf;
use Dompdf\Options;

require __DIR__ . "/vendor/autoload.php";

$options = new Options();
$options->set('isRemoteEnabled', TRUE);
$options->set('tempDir', '/tmp');
$options->set('chroot', __DIR__);

$dompdf = new Dompdf($options);
$html=$_POST['pdf_data'];
$email_id=$_POST['email_id'];
$cc=$_POST['cc'];
$sub=$_POST['sub'];
$message=$_POST['message'];
$quotation=$_POST['quotation'];

$dompdf->setPaper('A3', 'portrait');

$dompdf->load_html($html);
$dompdf->render();
$pdf_string =   $dompdf->output();

if($sub=="PURCHASE ORDER"){
    $name="PURCHASE_ORDER.pdf";
    
}

if($sub=="QUOTATION"){
    $name="QUOTATION.pdf";
    
}

if($sub=="Proforma Invoice"){
    $name="Proforma_Invoice.pdf";
    
}

file_put_contents($name, $pdf_string); 

$sql="select  * from smtp_details";
$query=mysqli_query($con,$sql);

if($result= mysqli_fetch_array($query)){
    $host=$result['host'];
     $port=$result['port'];
      $uname=$result['uname'];
       $pwd=$result['pwd'];
}



use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

$mail = new PHPMailer;

//Enable SMTP debugging. 
//$mail->SMTPDebug = 2;                               
//Set PHPMailer to use SMTP.
$mail->isSMTP();            
//Set SMTP host name                          
$mail->Host = $host;
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;                          
//Provide username and password     
$mail->Username = $uname;                 
$mail->Password = $pwd;                           
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "tls";                           
//Set TCP port to connect to 
$mail->Port = $port;                                   

$mail->From = $uname;


$cc = explode(',', $cc);
for($i=0;$i<count($cc);$i++){
    if($cc[$i]!=""){
$mail->AddCC($cc[$i]);
    }
}

$mail->FromName = "Amul Polymers";

$email_id = explode(',', $email_id);
for($i=0;$i<count($email_id);$i++){
    if($email_id[$i]!=""){
$mail->addAddress($email_id[$i]);
    }
}

$mail->isHTML(true);

 $mail->AddAttachment($name, '', $encoding = 'base64', $type = 'application/pdf');
        
$mail->Subject = $sub;
$mail->Body = $message;
$result1 =  $mail->send(); //echo $host;die();
if($result1==true){
    $sql="update quotation_details set email_status='sent' where quotation_no='".$quotation."'";
    mysqli_query($con,$sql);
    echo 1;
}else{
    echo "Sending mail failed";
}
?>