<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('valid_email'))
{
	function valid_email($address)
	{
		return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $address)) ? FALSE : TRUE;
	}
} 

function customized_email($data_array = array()){
	if((!is_array($data_array) && empty($data_array)) || (!is_array($data_array['to_email_ids']) && empty($data_array['to_email_ids'])))
		return;
	if(!isset($CI))
		$CI=&get_instance();	
	$email_config = array(
		'protocol' => 'smtp',
		'smtp_host' => 'smtp.gmail.com',
		'smtp_port' => 26,
		'smtp_user' => '',
		'smtp_pass' => '',
		'newline' => "\r\n",
		'crlf' => "\r\n",
		//'smtp_timeout' => '15'
	);
	$CI->load->library('email');
	$CI->email->initialize($email_config);
	$email_log_array=array();
	$email_log_array['from_email_id']= 'shreyas@nityam.in';
	$CI->email->from('shreyas@nityam.in','Shreyas');
	foreach($data_array['to_email_ids'] as $k=>$v){
		if(valid_email(trim($v))){
			$CI->email->to(trim($v));
		}else{
			unset($data_array['to_email_ids'][$k]);
		}	
	}
	$to_email_ids = implode(',',$data_array['to_email_ids']);
	$email_log_array['to_email_ids'] = $to_email_ids;
	
	if(isset($data_array['cc_email_ids']) && !empty($data_array['cc_email_ids'])){
		foreach($data_array['cc_email_ids'] as $k=>$v){
			if(valid_email(trim($v))){
				
				$CI->email->cc(trim($v));
			}else{
				unset($data_array['cc_email_ids'][$k]);
			}
		}
		$CI->email->cc($data_array['cc_email_ids']);
		$cc_email_ids = implode(',',$data_array['cc_email_ids']);
		$email_log_array['cc_email_ids'] = $cc_email_ids;
	}
	
	if(isset($data_array['bcc_email_ids']) && !empty($data_array['bcc_email_ids'])){
		foreach($data_array['bcc_email_ids'] as $k=>$v){
			if(valid_email(trim($v))){
				$CI->email->bcc(trim($v));
			}else{
				unset($data_array['bcc_email_ids'][$k]);
			}	
		}
		$bcc_email_ids = implode(',',$data_array['bcc_email_ids']);
		$email_log_array['bcc_email_ids'] = $bcc_email_ids;
	}
	
	$email_header = '<table width="600" align="center" cellpadding="0" cellspacing="0" style="font-family:arial; font-size:12px; line-height:18px; border:1px solid #830705;">';
	$email_header .= '<tr>';
	$email_header .= '<td><img src="'.base_url().'assets/images/email/header_new_nicb.jpg" width="600" height="100" /></td>';
	$email_header .= '</tr>';
	$email_header .= '<tr>';
	$email_header .= '<td bgcolor="#f6e7a2" style="border-top:1px solid #810505; border-bottom:1px solid #810505;padding:10px;">';
	$email_footer = '</td>';
    $email_footer .= '</tr>';
	$email_footer .= '</table>';
	if(isset($data_array['path']) && $data_array['path']!='')
	{
		$CI->email->attach($data_array['path']); 
	}
	$CI->email->subject($data_array['email_subject']); 
	$email_subject = $data_array['email_subject'];
	$email_log_array['email_subject'] = $email_subject;
	$CI->email->message($email_header.$data_array['email_message'].$email_footer);
	$email_message = $data_array['email_message'];
	$email_log_array['email_message'] = $email_header.$email_message.$email_footer;
	$CI->email->send();
	
	$debugging_text = $CI->email->print_debugger();	
	$email_log_array['debugging_text'] = $debugging_text;
	
	// if(isset($email_log_array) && !empty($email_log_array))
	// 	$CI->db->insert(TABLE_EMAIL_LOG, $email_log_array);
}