<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

include APPPATH . 'third_party/mail/class.phpmailer.php';
include APPPATH . "third_party/mail/class.smtp.php";

class Sendmail {

    public function send($toparam, $subject, $html_body, $attachment = NULL) {

        try {
            $CI = & get_instance();
            $CI->db->select('*', FALSE);
            $CI->db->from('app_email_setting');
            $email_datat = $CI->db->get()->row_array();

            $smtp_host = isset($email_datat['smtp_host']) ? $email_datat['smtp_host'] : "smtp.gmail.com";
            $smtp_username = isset($email_datat['smtp_username']) ? $email_datat['smtp_username'] : "test@xyz.com";
            $smtp_password = isset($email_datat['smtp_password']) ? $email_datat['smtp_password'] : "password";
            $smtp_port = isset($email_datat['smtp_port']) ? $email_datat['smtp_port'] : 587;
            $smtp_secure = isset($email_datat['smtp_secure']) ? $email_datat['smtp_secure'] : "tls";
            $email_from = isset($email_datat['email_from']) ? $email_datat['email_from'] : '';

            $CI->load->library('email');

            $config['protocol'] = "smtp";
            $config['smtp_host'] = $smtp_host;
            $config['smtp_port'] = $smtp_port;
            $config['smtp_crypto'] = $smtp_secure;
            $config['smtp_user'] = $smtp_username;
            $config['smtp_pass'] = $smtp_password;

            $config['charset'] = "utf-8";
            $config['mailtype'] = "html";
            $config['newline'] = "\r\n";
            $config['wordwrap'] = TRUE;
            $config['validate'] = FALSE;
            $config['priority'] = 3;
            $config['crlf'] = "\r\n";

            if (isset($toparam) && count($toparam) > 0) {
                $to_email = $toparam['to_email'];
                $to_name = $toparam['to_name'];
            }

            $CI->email->initialize($config);
            $CI->email->from($email_from, dt_get_CompanyName());
            $CI->email->to($to_email);
            $CI->email->subject($subject);
            $CI->email->message($html_body);

            if (isset($attachment) && $attachment!=null){
                $CI->email->attach($attachment);
            }

            $app_email_log['email'] = $to_email;
            $app_email_log['name'] = $to_name;
            $app_email_log['subject'] = $subject;
            $app_email_log['content'] = $html_body;
            $app_email_log['created_date'] = date("Y-m-d H:i:s");

            if ($CI->email->send()) {
                $app_email_log['details'] = 'Sent';
                $app_email_log['status'] = 'Sent';
                $CI->db->insert('app_email_log', $app_email_log);
                return true;
            } else {
                $app_email_log['details'] = json_encode($CI->email->print_debugger());
                $app_email_log['status'] = 'Not Sent';
                $CI->db->insert('app_email_log', $app_email_log);
                return false;
            }
        }catch (\Exception $exception){

        }
    }
}
