<?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'};
$email = $obj->{'emailttt'};

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

    
    $textb = null;
    $textb .= "Tests Email<br/><br/>";
  
    $textb .= "Warm regards,<br/><br/>";

    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 2024");// Add no-reply address
    $mail->Subject = "Orientation to CSO Week 2024";

    $mail->IsHTML(true); 
    $mail->Body = $textb;
    $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",
            "email"=>$email,
            "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(); 
}
