<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';


$destination = "logs/faic_mail" . date('Y-m-d') . ".log";


$data = json_decode(file_get_contents("php://input"), true); // collect input parameters and convert into readable format

$api_key  =  $_POST['api_key'];
$mail_body  =    $_POST['mail_body'];
$send_to  =  $_POST['send_to'];
$mail_subject  =  $_POST['mail_subject'];
$file_namepp = $_POST['file_name'];

//$fileName  =  $_FILES['file']['name'];
$tempPath  =  $_FILES['file']['tmp_name'];
$fileSize  =  $_FILES['file']['size'];

$fileName =  time().'.pdf';

$upload_path = ''; // set upload folder path 
	
$fileExt = strtolower(pathinfo($fileName,PATHINFO_EXTENSION)); // get image extension		
            
//check file not exist our upload folder path
if(!file_exists($upload_path . $fileName))
{
    // check file size '5MB'
    if($fileSize < 50000000){
        move_uploaded_file($tempPath, $upload_path . $fileName); // move file from system temporary path to our upload folder path 
    }
    else{		
        $errorMSG = json_encode(array("status"=>"217","statusDesc" => "Sorry, your file is too large, please upload 5 MB size"));	
        echo $errorMSG;
    }
}
else
{		
    $errorMSG = json_encode(array("status"=>"216","statusDesc" => "Sorry, file already exists check upload folder"));	
    echo $errorMSG;
}

if($api_key=='YTc4YWY3ZWVlZWViODU3YTY5ODUzNTA4ZGU3YzVhYzM1NTdjNjM1MWEyYzA1ODU1ZDhlqA12AjkP0qNTc4ZjU3N2QwMDVmMw==')
{

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer();

try {

    //Server settings
    $mail->SMTPDebug = 0;//SMTP::DEBUG_SERVER;                      //Enable verbose debug output
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'smtp.gmail.com';                     //Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = 'faicconference@gmail.com';                     //SMTP username
    $mail->Password   = 'kkxdcmgeqpyxxdzi';                               //SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
    $mail->Port       = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

    //Recipients
    $mail->SetFrom("faicconference@gmail.com","FAIC2024");
    $mail->AddAddress("dietrich.mchami@bluefinsolutions.co.tz");    
    //Add a recipient

    //Attachments
    $mail->addAttachment($fileName,$file_namepp);     //Add attachments    
    //Optional name

    //Content
    //$mail->isHTML(true);                        //Set email format to HTML
    $mail->Subject = $mail_subject;
    $mail->Body = $mail_body;
    $mail->AddAddress($send_to);

    $mail->send();
    $response=array(
        "status"=>"200",
        "statusDesc"=>"Success");
       
        $response=json_encode($response);
        //error_log("\n" . date('Y-m-d H:i:s') . " Response :\n $response ", 3, $destination);
        print_r($response);
        die(); 
} catch (Exception $e) 
{
    $error = 'Mail error: ' . $mail->ErrorInfo;
        $response=array(
        "status"=>"202",
        "statusDesc"=>$error );
    
    $response=json_encode($response);
    //error_log("\n" . date('Y-m-d H:i:s') . " Response :\n $response ", 3, $destination);
    print_r($response);
    die();
    
}

}
else
{
    $response=array(
        "status"=>"201",
        "statusDesc"=>"Invalid API KEY"        
    );
    $response=json_encode($response);
    //error_log("\n" . date('Y-m-d H:i:s') . " Response :\n $response ", 3, $destination);
    print_r($response);
    die(); 
}


