<?
	$res = $cat_db->query("select * from cat_mail_tasks where state = 0");
	
	foreach($res as $r) {

		$qes = $cat_db->query("select * from cat_domains where id_domains='$r[to]'");

		$table = $qes[0]['in_table'];
		$field = $qes[0]['name'];

		if(strlen($r['to_limit']) > 1)
			$qes = $cat_db->query("select * from $table where ".$r['to_limit']);
		else
			$qes = $cat_db->query("select * from $table");

		foreach($qes as $q) {
			if(!($q['enable'] == '0')) {
				$head = "From: ".$r['from']."\n";
				$head .= "To: ".$q[$field]."\n";
				$head .= "Subject: =?Windows-1251?Q?".imap_8bit($r['subj'])."?=\n";
				$head .= "Reply-To: ".$r['from']."\n";
				$head .= "Mime-Version: 1.0\n";
				$head .= "Content-Type:multipart/mixed;";
				$head .= "boundary=\"----------".$r['uid']."\"\n\n";
		        	
				@mail("", "", $r['data'], $head);	
			}
		};		

		$cat_db->query("update cat_mail_tasks set state='1' where task_id='$r[task_id]'");
	}


?>