<?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'};

$textb=null;
$textb.="Hello ".$user_name."<br/>";
$textb.="We have received your registration to attend ITAC 2024 on August 6th & 7th 2024 at Gran Melia Hotel - Arusha.<br/><br/>";
$textb.="You will receive confirmation to attend the conference once you have made a registration deposit of 25,000 Tanzanian Shillings (TZS) for Tanzanian residents and 50 USD for non-residents.<br/><br/>";
$textb.="The payment should be made through one of the following bank accounts:<br/><br/>";
$textb.="Bank name: NMB PLC<br/>";
$textb.="Branch - Bank House<br/>";
$textb.="Account Name: WAJIBU - Institute of Public Accountability<br/>";
$textb.="Account Number: 20110018487 (TShs)<br/>";
$textb.="Account Number: 20110018488 (USD)<br/>";
$textb.="SWIFT CODE: NMIBTZTZ<br/><br/>";
$textb.="Please note that; After making payment send us the proof of payment to the following email address; info@wajibu.or.tz<br/>";


/*function attachFromUrl(PHPMailer $mail, $url, $name) {
    $fileContent = file_get_contents($url);
    $mail->addStringAttachment($fileContent, $name);
}*/

//$webFileUrl = $invoince; // Replace with your web link
//$attachmentName = $ref_pay_invo.'.pdf';

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 = "info@wajibu.or.tz";
    $mail->Password = "govtxbicncmhoxsa";
    $mail->SetFrom("info@wajibu.or.tz", "ITAC Conference 2024");// Add no-reply address
    $mail->Subject = "ITAC Conference 2024";

    //attachFromUrl($mail, $webFileUrl, $attachmentName);

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