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

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

$json = file_get_contents('php://input');
$obj = json_decode($json);

//error_log("\n" . date('Y-m-d H:i:s') . " Incoming Request :\n $json ", 3, $destination);

$api_key = $obj->{'api_key'};
$user_name = $obj->{'user_name'};
$email = $obj->{'emailttt'};
$invoicenum = $obj->{'invoicenum'};
$invoince=$obj->{'obj'};
$ref_pay_invo=$obj->{'ref_pay_invo'};


if ($api_key == 'YTc4YWY3ZWVlZWViODU3YTY5ODUzNTA4ZGU3YzVhYzM1NTdjNjM1MWEyYzA1ODU1ZDhlqA12AjkP0qNTc4ZjU3N2QwMDVmMw==') {
    global $error;
    $mail = new PHPMailer();  // 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 = "csoweek@gmail.com";
    $mail->Password = "tvycktczujtyxezc";
    $mail->SetFrom("information@thefoundation.or.tz", "CSOWEEK 2025 Registration");// Add no-reply address
    $mail->Subject = "CSOWEEK 2025 Registration";
    $mail->IsHTML(true); 
    $mail->Body = "Greetings ".$user_name." <br/>Your Registration Has been successfully received, reference number <span style=\"font-weight: bold; font-family: 'Roboto', sans-serif;\">".$ref_pay_invo."</span> has been generated for you.Kindly Use the Reference number to complete payment process. Attached to this email is your invoice.<br/>" ."<br/>Best Of Regards";
    $url = $invoince;
    $file_content = file_get_contents($url);
    $filename = basename($url);
    $mail->addStringAttachment($file_content, $filename);
    $mail->AddAddress($email);
    
    // Remove unnecessary headers
    $mail->addCustomHeader("X-Mailer", "");  // Removes X-Mailer header
    $mail->addCustomHeader("X-PHP-Originating-Script", "");  // Removes X-PHP-Originating-Script header

    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(); 
}