<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Email extends CI_Controller {
	public function __construct()
	{
		parent::__construct();
		session_start();
		
		date_default_timezone_set('Asia/Singapore');
		$this->load->model('edm_m');
	}
	
	public function index2()
	{	
		print '1';
	}
	 
	public function index()
	{			
		
		$edms = $this->edm_m->pending_edm_list();
	
		
		foreach($edms as $s)
		{
		
			$listing = $this->edm_m->pending_list($s['id']);
			

			$dd['status'] = 2;
			$this->edm_m->update($s['id'],$dd);
			foreach($listing as $a)
			{
					$blast_info = $this->edm_m->info($a['blast_id']);
					$data['content'] = $blast_info['content'];
					
					$html = '';
					$html = $this->load->view('emails/mass-email.php',$data,TRUE);
				
					if($blast_info['edm_type'] == 1)
						$url = site_url('form/viewlink1/'.$a['code'].'/'.$a['company_code']);

					
					$html = str_replace('[NAME]','Dear '.$a['name'],$html);
					$html = str_replace('[EMAIL]',$a['email'],$html);
					$html = str_replace('[COMPANY]',$a['company_name'],$html);
					$html = str_replace('[DATE]',date('j M Y'),$html);
					$html = str_replace('[URL]',$url,$html);				
				
			
					$from = $this->config->item('from');
					$username = $this->config->item('username');
					$password = $this->config->item('password');
					$host = $this->config->item('host');
					$port = $this->config->item('port');
					$from_name = $this->config->item('from_name');
					$subject = $blast_info['subject'];
					$to = '';
					$to = $a['email'];	
					$cc = '';
					$cc = $a['cc_emails'];	
					$cc = $cc.',replies@imas.org.sg';		
			
					if($to != '')
					{
						send_email($to,$host,$port,$username,$password,$from,$from_name,$subject,$html,$file,$filename,$file,$filename,$cc);
						$id = $a['id'];
						$d['datetime_sent'] = date('Y-m-d H:i:s');
						$d['email_content'] = $html;
						$this->edm_m->update_email_blast($id,$d);
					}
			}
			
			
			$dd['status'] = 3;
			$this->edm_m->update($s['id'],$dd);
		}
	}
	
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */