<?php



require( './wp-load.php' );

date_default_timezone_set('Asia/Singapore');

// Hooking up functions to the correct WordPress filters 

add_filter( 'wp_mail_from', 'change_default_sender_email' );

add_filter( 'wp_mail_from_name', 'change_default_sender_name' );



$subject = 'Certificate of Attendance for SPS Symposium';



function pdf_create($html, $filename, $stream = FALSE)

{

    require_once( 'membership-admin/system/helpers/mpdf/mpdf.php');



	$mpdf=new mPDF('utf-8', 'A4-L', 0, '', 10, 10, 10, 10, 10, 10);



	$mpdf->SetFont('Arial');



    $mpdf->WriteHTML($html);



    if ($stream)

    {

        $mpdf->Output($filename . '.pdf', 'D');

    }

    else

    {

        $mpdf->Output('./attendance_cert/' . $filename . '.pdf', 'F');

        



        return './attendance_cert/' . $filename . '.pdf';



    }

}



// Path to your CSV file

$filename = 'cert.txt'; // Make sure this file exists

$d = array();

// Open the file for reading

if (($handle = fopen($filename, "r")) !== false) {

    // Read each line of the file

    while (($data = fgetcsv($handle, 0, "\t")) !== false) {

        // Print the data (as an array)

        $d[] = $data;

    }

    fclose($handle);

} else {

    echo "Failed to open file: $filename";

}





foreach($d as $count=>$da)

{

	$name = '';

	$email = '';

	

	$name = $da[0];

	$email = $da[1];


	

	if($count == 364)

	{

		$output = '';

		$template = '';

		$template = file_get_contents('cert.html');

		

		// Array of variables to replace

		$variables = [

			'[NAME]' => $name,

		];

		



		// Replace placeholders with actual values

		$output = str_replace(array_keys($variables), array_values($variables), $template);

		

		

		$filename = 'Certificate-of-Attendance-'.($count+1);





		$filename2 = $filename.'.pdf';

		



		pdf_create($output, $filename);



		$to = $email;

		$body = "";

		$body = "

			<p>

			Dear ".$name.",</p>

		<p>

		Thanks once again for attending our inaugural SPS Symposium. Please find your Certificate of Attendance attached. Hope to see you at the next symposium!

		</p>

		<p>

		Warm Regards,

		<br>

		Singapore Psychological Society

		</p>

		<p>

		8 Eu Tong Sen Street #18-81 The Central Singapore 059818 | Email: <a href='mailto:secretariat@singaporepsychologicalsociety.org;'>secretariat@singaporepsychologicalsociety.org</a>

		</p>

			";

		$headers = array('Content-Type: text/html; charset=UTF-8');

		$pdf_file_path = './attendance_cert/'.$filename.'.pdf'; // Adjust this path accordingly



		$attachments = array($pdf_file_path,'cert.pdf'); // Array of attachment paths

		if($to != '')

		{

			wp_mail( $to, $subject, $body, $headers,$attachments );



		}

		

		sleep(1);

	}

}

print 'DONE';



?>

