<?php

namespace App\Models;

use CodeIgniter\Model;

class EmailModel extends Model
{
    protected $db;
    protected $setting            = 'application_setting';
    
    function get_app_setting()
    {
        return $this->db->table($this->setting)
       ->where('id', 1)
       ->get()
       ->getRow();
    }
    function send_email($to, $subject, $message, $mailType, $title = '', $mobile = '')
    {
        $email = \Config\Services::email();
        $setting = $this->get_app_setting();
        $config['protocol']     = $setting->protocol;
        $config['SMTPHost']     = $setting->host;
        $config['SMTPUser']     = $setting->username;
        $config['SMTPPass']     = $setting->password;
        $config['SMTPPort']     = $setting->port;
        $config['SMTPCrypto']   = $setting->crypto;
        $config['mailType']     = $mailType;
        $email->initialize($config);

        $email->setTo($to);
        $email->setFrom($setting->username, $title);
        $email->setSubject($subject);
        $email->setMessage($message);
        if($mobile)
        {
            $email->attach('uploads/pdf/id_card_'.$mobile.'.pdf');
        }
        

        if ($email->send()) 
        {
            return 2;
        } 
        else 
        {
            return $email->printDebugger();
        }

    }
}
