<?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'};
$first_name = $obj->{'first_name'};
$second_name = $obj->{'second_name'};
$last_name = $obj->{'last_name'};
$name_of_employer=$obj->{'name_of_employer'};
$email=$obj->{'email'};

$professional_designation=$obj->{'professional_designation'};
$nationality=$obj->{'nationality'};
$physical_address=$obj->{'physical_address'};
$mobile_number=$obj->{'mobile_number'};
$identification=$obj->{'identification'};

$ssemail=$obj->{'ssemail'};

$dashr=' ';


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


$textb=null;
$textb.="Hello <br/><br/>";
$textb.="We have received a new request registration to acess resource centre, Kindly see the below details.<br/><br/>";
$textb.="Full Name:".$first_name.$dashr.$second_name.$dashr.$last_name."<br/>";
$textb.="Name Of Employee:".$name_of_employer."<br/>";
$textb.="Professional Designation:".$professional_designation."<br/>";
$textb.="Nationality:".$nationality."<br/>";
$textb.="Physical Address:".$physical_address."<br/>";
$textb.="Mobile Number:".$mobile_number."<br/>";
$textb.="Identification:".$identification."<br/><br/>";
$textb.="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 = "info@wajibu.or.tz";
    $mail->Password = "govtxbicncmhoxsa";
    $mail->SetFrom($ssemail, "Resource Center Registraion");// Add no-reply address
    $mail->Subject = "Resource Center Registraion";

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