<?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 = strtoupper($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==') {


    $textb=null;
    $textb.="Dear ".$user_name."<br/><br/>";
    $textb.="<span style=\"text-align: justify;text-justify: inter-word;\">We are delighted to confirm your registration for the International Transparency and Accountability Conference (ITAC) 2024. Thank you for your prompt payment of the participation fee.</span><br/><br/>";
    $textb.="<span style=\"font-weight: bold;text-align: justify;\">Event Details:</span><br/><br/>";
    $textb.="<span style=\"font-weight: bold;text-align: justify;\">Dates:</span>  6th - 7th  August, 2024<br/><br/>";
    $textb.="<span style=\"font-weight: bold;text-align: justify;\">Venue:</span> Gran Melia Hotel, Arusha, Tanzania<br/><br/>";
    
    $textb.="<span style=\"text-align: justify;text-justify: inter-word;\">This year's conference, themed \"Fostering Effective Public Procurement for Sustainable Development in Africa,\" promises to be an enriching and insightful event. As a confirmed participant, you will join a diverse group of procurement practitioners, government officials, policymakers, academics, and civil society representatives. Together, we will explore critical issues in public procurement and work towards innovative solutions to enhance transparency, accountability, and sustainable development in Africa.</span><br/><br/>";
    
    $textb.="<span style=\"font-weight: bold;text-align: justify;\">Conference Highlights:</span><br/><br/>";
    
    $textb .= "<ul style=\"text-align: justify;text-justify: inter-word;\">";
    $textb .= "<li><span style=\"font-weight: bold;\">Engaging Keynote Speeches:</span> Hear from leading experts in the field of public procurement.</li>";
    $textb .= "<li><span style=\"font-weight: bold;\">Interactive Panel Discussions:</span> Participate in discussions on modernizing procurement practices, leveraging technology for transparency, enhancing public-private partnerships, and more.</li>";
    $textb .= "<li><span style=\"font-weight: bold;\">Networking Opportunities:</span> Connect with fellow professionals, share experiences, and build lasting relationships.</li>";
    $textb .= "<li><span style=\"font-weight: bold;\">Practical Workshops:</span> Gain hands-on experience and practical knowledge to apply in your professional role.</li>";
    $textb .= "</ul>";
    $textb.="<span style=\"text-align: justify;text-justify: inter-word;\"><span style=\"font-weight: bold;\"><br/>Additional Information:</span></span><br/><br/>";
    
    $textb .= "<span style=\"text-align: justify;text-justify: inter-word;\">All participants will receive conference materials upon arrival at the venue. Should you have any questions or require further assistance, please do not hesitate to contact our event team at <a href=\"mailto:info@wajibu.or.tz\">info@wajibu.or.tz</a> or +255736 666 916 / +255757431777 / +255762882207</span><br/><br/>";

    $textb.="<span style=\"text-align: justify;text-justify: inter-word;\">We look forward to welcoming you to ITAC 2024 and are confident that your participation will contribute to the success of this significant event.</span><br/><br/>";
    
    $textb.="Warm regards,<br/><br/>";
    
    $textb.="<span style=\"font-weight: bold;text-align: justify;\">WAJIBU Team,</span><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("info@wajibu.or.tz", "ITAC Conference 2024");// Add no-reply address
    $mail->Subject = "Confirmation of Participation for ITAC 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(); 
}