<?php
if(!(is_readable(realpath(dirname(__FILE__)).'/mail.inc'))) {
	die('Error - Could not open or read file '.realpath(dirname(__FILE__)).'/mail.inc');
} else {
	include_once(realpath(dirname(__FILE__)).'/mail.inc');
}

if(!(is_readable(realpath(dirname(__FILE__)).'/db/mysqlroutines.mail.inc'))) {
	die('Error - Could not open or read file '.realpath(dirname(__FILE__)).'/db/mysqlroutines.mail.inc');
} else {
	include_once(realpath(dirname(__FILE__)).'/db/mysqlroutines.mail.inc');
}

function  sendMailsFromQueue($type = NULL, $limit = 1000){
	$cn = db_connect();
	$mailqueue = db_getMailQueue($cn, $type, $limit);
	foreach ($mailqueue as $mail) {
		if (smtpmail( $mail['mq_recipients'], $mail['mq_subject'], $mail['mq_body'],  $mail['mq_replyto'])){
			db_deleteMailFromQueue($cn, $mail['idmailqueue']);
			logmailevent('Email sent to '.$mail['mq_recipients'].' with the '.$mail['mq_subject'].' subject');
		} else {
			db_getUpdateMailQueuePriority($cn, $mail['idmailqueue']);
		}
	}
}

?>
