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

@require_once 'mail_master/PHPMailerAutoload.php';
@set_time_limit(0);

$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'];

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

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

$upload_path = 'uploads/'; // 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==')
{
    global $error;
    $mail = new PHPMailer(true);  // create a new object
    $mail->IsSMTP(); // enable SMTP
    $mail->SMTPDebug = 0;  // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true;  // authentication enabled
    $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
    $mail->Host = 'smtp.gmail.com'; //
    $mail->Port = 465;
    $mail->Username = "faicconference@gmail.com";
    $mail->Password = "kkxdcmgeqpyxxdzi";
    $mail->SetFrom("faicconference@gmail.com","FAIC2024");

    $mail->addAttachment("/uploads/iii.pdf");

    $mail->Subject = $mail_subject;
    $mail->IsHTML(true); 
    $mail->Body = $mail_body;
    $mail->AddAddress($send_to);

    //$file_to_attach = '1708175969.pdf';
    //Attachments
    //Send Email
    if (!$mail->Send()) {
        $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"=>"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(); 
        
    }
}
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(); 
}



