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

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==') {

    $urlot="https://csoweek.or.tz/Register";
    $textb=null;
    $textb.="Dear ".$user_name."<br/><br/>";
    $textb.="<span style=\"text-align: justify;text-justify: inter-word;\">Thank you for registering for #CSOWEEK2025, which will be held from September 9th to 13th in Arusha! We're thrilled that you've decided to join us for this exciting event, where leaders, innovators, and change-makers in the civil society sector will come together for a week of collaboration and learning.</span><br/><br/>";
    
    $textb.="<span style=\"text-align: justify;text-justify: inter-word;\">It looks like you've started the registration process but haven't yet completed your payment. To secure your spot, please finalize your payment as soon as possible. Finalizing your payment will ensure your place at #CSOWEEK2025, where you'll have the opportunity to connect with peers, participate in insightful discussions, and gain valuable knowledge to advance your work.</span><br/><br/>";
    
    $textb .= "<span style=\"text-align: justify;text-justify: inter-word;\">If you need any assistance with the payment process or have any questions, our team is here to help. Please don't hesitate to reach out to us at <a href=\"mailto:information@thefoundation.or.tz\">information@thefoundation.or.tz</a>  or +255 22 - 2664890/ +255 22 - 2664891/+255 22 - 2664892/+255 22 - 2664893.</span><br/><br/>";

    $textb.="<span style=\"text-align: justify;text-justify: inter-word;\">To finalize your payment kindly visit <a href=".$urlot.">https://csoweek.or.tz/Register</a></span><br/><br/>";
    $textb.="<span style=\"text-align: justify;text-justify: inter-word;\">We look forward to welcoming you to Arusha for what promises to be an inspiring and impactful week!</span><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 2025 Registration");// Add no-reply address
    $mail->Subject = "Complete Your Registration for #CSOWEEK2024 - Finalize Your Payment Today!";

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