<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once(APPPATH . 'core/My_Model.php');
require_once APPPATH.'third_party/PHPMailer/class.phpmailer.php';
require_once APPPATH.'third_party/PHPMailer/class.smtp.php';

class Sendmails_model extends My_Model {
	public function __construct(){
		parent::__construct();
	}

	protected $_config = array(
		'protocol' => 'smtp',
		'smtp_host' => 'smtp.office365.com',
		'smtp_port' => 587,
		'smtp_user' => 'office@techfabindia.com',
		'smtp_pass' => 'dbtzqjvkllnrdpgp'
	);

	public function htmlmail($data){
		$mail = new PHPMailer();

		$mail->isSMTP();
        if($this->config->item('env') == "production"){
        	$mail->Host     = "localhost";
        	$mail->SMTPAuth = false;
        }else{
        	$mail->Host     = $this->_config["smtp_host"];
        	$mail->SMTPAuth = true;
        }
        $mail->Username = $this->_config["smtp_user"];
        $mail->Password = $this->_config["smtp_pass"];
        $mail->SMTPSecure = 'tls';
        $mail->Port     = $this->_config["smtp_port"];
       	//$mail->SMTPDebug = 3;

       	

       	$mail->SMTPOptions = array(
		    'ssl' => array(
		        'verify_peer' => false,
		        'verify_peer_name' => false,
		        'allow_self_signed' => true
		    )
		);

		$setFrom = $this->_config["smtp_user"];
		$setName = "Team TechFab India";
		if(isset($data['set_from']) && !empty($data['set_from'])){
			$setFrom = $data['set_from'];
		}
		if(isset($data['set_name']) && !empty($data['set_name'])){
			$setName = $data['set_name'];
		}
        
        $mail->setFrom($setFrom, $setName);
        $mail->addReplyTo($setFrom, $setName);
        
        // Add a recipient
        $mail->addAddress($data['email']);

        if($this->config->item('env') == "production"){
			if(isset($data['bcc_email']) && !empty($data['bcc_email'])){
				foreach($data['bcc_email'] as $emailKey=>$bcc_email_ids){
					$mail->addBCC($bcc_email_ids);
				}
			}

			if (isset($data['cc_email']) && is_array($data['cc_email'])) {
		        foreach ($data['cc_email'] as $cc_email) {
		            $mail->addCC($cc_email);
		        }
		    }
		}


		
		

		if (isset($data["attachment"]) && is_array($data["attachment"])) {
            foreach ($data["attachment"] as $attachment) {
                $mail->addAttachment((string)$attachment);
            }
            $email_data["attachment"] = json_encode($data["attachment"]);
	    }elseif (isset($data["attachment"]) && !empty($data["attachment"]) && $data["attachment"] != "") {
	    	$mail->addAttachment($data["attachment"]);
	    	$email_data["attachment"] = $data["attachment"];
	    }

        $mail->Subject = $data['subject'];
        
        // Set email format to HTML
        $mail->isHTML(true);
        
        // Email body content
        $mailContent = $this->load->view($data["email_view"],$data,TRUE);
        $mail->Body = $mailContent;

        $email_data["sender_email"] = $data["email"];
        $email_data["email_subject"] = $data["subject"];
        $email_data["email_body"] = $mailContent;
        $email_data["created_at"] = Date("Y-m-d H:i:s");

        $email_response = 1;
        
        // Send email
        if(!$mail->send()){
            $email_response = $mail->ErrorInfo;
        }

        $email_data["email_response"] = $email_response;

        $this->db->insert('hc_email_logs',$email_data);

        return $email_response;
	}

	public function send_email($data){
		$config = array(
			'protocol' => 'smtp',
			'smtp_host' => 'tls://smtp.office365.com',
			'smtp_port' => 587,
			'smtp_user' => 'office@techfabindia.com',
			'smtp_pass' => 'Foh296171',
			'mailtype' => 'html',
			'charset' => "utf-8"
		);
		$this->email->initialize($config);


		$this->email->set_newline("\r\n");
		$this->email->set_mailtype("html");
		$this->email->from('office@techfabindia.com', 'Tech Fab India');
		$this->email->to($data['email']);

		$mailContent = $this->load->view($data["email_view"],$data,TRUE);
		
		$this->email->subject($data['subject']);
		$ans = $this->email->message($mailContent);
		$result = $this->email->send();
		print_r($this->email->print_debugger());
		print_r($result);
	}
}

/* End of file Sendmails_model.php */
/* Location: ./application/models/Sendmails_model.php */