<?php

error_reporting(E_ALL);
ini_set('display_errors',1);

date_default_timezone_set('Asia/Singapore');

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

// 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' );

$querystr = "SELECT * FROM insurance_payment WHERE paid = 1 AND email_sent = 0";	
$to_be_sent = $wpdb->get_results($querystr, OBJECT);	

foreach($to_be_sent as $r)
{
	$to = '';
	$to = $r->email;

	$datetime_email_sent = date('Y-m-d H:i:s',strtotime('+8 hours'));
	
	$name = $r->first_name.' '.$r->last_name;
	
	$date = date('j F Y',strtotime($r->payment_datetime));
	
	$subject = 'Thank you for purchasing/renewing the Professional Indemnity. ';
	$body = "
	Dear ".$name.",<br>
	<p>
<strong>Here are the details of your payment:</strong>
</p>
<p>
Payment Amount: $".number_format($r->amount,2)."<br>
Transaction ID: ".$r->transaction_no."<br>
Payment Date: ".$date."<br>
</p>
<p>
We have received your payment. You may check in your Dashboard in January for the Professional Indemnity Certificate. It will be uploaded to your account after all applications have been processed. 
</p>
<p>
Should you have any queries, please feel free to write to our Secretariat at <a href='mailto:secretariat@singaporepsychologicalsociety.org'>secretariat@singaporepsychologicalsociety.org</a>
</p>
<p>
<strong>Regards,</strong><br>
Singapore Psychological Society
</p>
<p>
8 Eu Tong Sen Street #18-81 The Central Singapore 059818 | Email: secretariat@singaporepsychologicalsociety.org
</p>
";
	$headers = array('Content-Type: text/html; charset=UTF-8');

	
	
	if($to != '')
		wp_mail( $to, $subject, $body, $headers );
	
	$update_q = "UPDATE insurance_payment SET email_sent = 1,datetime_email_sent= '".$datetime_email_sent."' WHERE id = ".$r->id;

	$wpdb->query($update_q);	

}