<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Emails extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('sendmails_model', 'send_mail');
	}

	public function index(){
		$this->db->where('status',0);
		$emails = $this->db->get('hc_emails_cron')->result();

		if(!empty($emails)){
			foreach($emails as $emailKey=>$emailVal){
				$email_data = json_decode($emailVal->information, TRUE);
				$email_data['email'] = $emailVal->sent_to;
				if(!empty($emailVal->bcc_email)){
					$email_data['bcc_email'] = json_decode($emailVal->bcc_email, TRUE);
				}
				$email_data['email_view'] = $emailVal->email_body;
				$email_data['subject'] = $emailVal->subject;
				$email_result = $this->send_mail->htmlmail($email_data);

				echo "<pre>";print_r($email_result);

				//Updated email response
				if($email_result === 1){
					$updateArr['status'] = 1;
				}
				$updateArr['response'] = $email_result;
				$updateArr['updated_at'] = date('Y-m-d H:i:s');
				$this->db->update('hc_emails_cron',$updateArr);
			}
		}
	}

	public function test(){
		$email_data['email'] = "sanket@hepta.me";
		//$email_data['cc_email'] = array("sanketkumbhar86@gmail.com", "sanket.hepta@gmail.com");
		$email_data['email_view'] = "emails/test";
		$email_data['subject'] = "Test Email";
		$email_result = $this->send_mail->htmlmail($email_data);

		echo "<pre>";print_r($email_result);
	}
}

/* End of file Emails.php */
/* Location: ./application/controllers/Emails.php */