<?php 

class EnquiryEmailShell extends Shell {
	
	var $uses = array('Enquiry', 'EnquiryEmailQueue');
	var $tasks = array('Email');
	var $Email;
	


	function main() {
		$queue_items = $this->EnquiryEmailQueue->find('all');
		
		foreach($queue_items as $queue_item) {
		echo $queue_item['Enquiry']['title'];
		$this->__sendNewEnquiryEmail($queue_item['Enquiry']['id']);
		}
		
		
				
	}
	
	
	function __sendNewEnquiryEmail($id) {
		
		//Setup the SMTP Options
		//$settings = array(
			/* 
		$this->Email->smtpOptions = array(
			'port' => '25',
			'timeout' => '15',
			'host' => '192.168.0.6');
		$this->Email->delivery = 'smtp';
		*/
		$enquiry = $this->Enquiry->read(null, $id);
		$this->Email->set('enquiry', $enquiry);
		$this->Email->settings(array(
			'subject' => $enquiry['Enquiry']['title'].' - Your Enquiry has been Entered in our System - CMC Technologies', 
			'replyTo' => $enquiry['User']['email'],
			'from' => 'CMC Technologies - Sales <sales@cmctechnologies.com.au>',
			'template' => 'enquiry_added',
			'sendAs' => 'both',
			'charset' => 'iso-8859-1'));
			
		$this->Email->send(array(
		'to' => $enquiry['Contact']['email'],
		'subject' => $enquiry['Enquiry']['title'].' - Your Enquiry has been Entered in our System - CMC Technologies',
		'cc' => array($enquiry['User']['email'])));
		echo "Email should have been sent to ".$enquiry['Contact']['email']."\n";
		//$this->Email->bcc = array('carpis@cmctechnologies.com.au');
		//'bcc' => array('carpis@cmctechnologies.com.au')
		/* 
		$this->Email->subject = $enquiry['Enquiry']['title'].' - Your Enquiry has been Entered in our System - CMC Technologies';
		$this->Email->replyTo = $enquiry['User']['email'];
		$this->Email->from = 'CMC Technologies - Sales <sales@cmctechnologies.com.au>';
		$this->Email->template = 'enquiry_added';
		$this->Email->sendAs = 'both';
		$this->Email->charset = 'iso-8859-1';
		if($this->Email->send()) {
				echo "Email sent successfully to".$enquiry['Contact']['email'];
				return 0; //No Errors.
			}
			else {
				echo "Something Went wrong";
				return 1; //Errors!
			}
		*/
	}
	
	
}	
	
?>	
	
