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

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

include(APPPATH . "helpers/mail_helper/Exception.php");
include(APPPATH . "helpers/mail_helper/PHPMailer.php");
include(APPPATH . "helpers/mail_helper/SMTP.php");

if (!function_exists('send_mail')) {
    /**
     * Send mail
     *
     * @param string $send_from
     * @param string $send_to
     * @return string  $send_name
     * @return string  $send_subj
     * @return string  $send_body
     */
    function send_mail($send_to = NULL,  $send_name = NULL, $cc_to = NULL, $send_subj = NULL, $send_body = NULL, $file_attach = NULL)
    {
        $CI = &get_instance();
        $sql = "SELECT * FROM company WHERE id = '" . $CI->session->userdata("user_profile")->company_id . "'";
        $company = $CI->db->query($sql)->row();
        $company_name = $company->company_name;
        $smtp_host = 'cpanel06wh.bkk1.cloud.z.com';
        $company_email = $company->company_email; // เมล บริษัท
        $hr_email = $company->hr_email; // เมล ของ HR จะแสดงในตอนส่งข้อมูล
        $smtp_user = $company->sender_email; // เมล ของ Company จะเป็นตัวส่งข้อมูล
        //$smtp_pass = $CI->app->decrypt_segment($company->sender_email_password, 'email'); // เมล ของ Company จะเป็นตัวส่งข้อมูล
        $smtp_pass = $CI->session->userdata("user_profile")->sender_email_password;
// echo $smtp_user; die();
        $mail = new PHPMailer(true);

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

        $mail->CharSet = "utf-8";
        $mail->SMTPAuth   = true;
        $mail->Host       = $smtp_host;
        //Set the SMTP port number - likely to be 25, 465 or 587
        $mail->Port       = 465;
        $mail->SMTPSecure = "ssl";
        $mail->Username   = $smtp_user;
        $mail->Password   = $smtp_pass;
        $mail->SMTPDebug  = 1;

        $err = array();

        $mail->setFrom($hr_email, $company_name);
        $mail->addAddress($send_to, $send_name);

        /* CC To */
        
        $mail->AddCC($company_email);
        $mail->AddCC($hr_email);
        
        foreach ($cc_to as $cc) {
            $mail->AddCC($cc[1],  $cc[0]);
        }

        $mail->Subject = $send_subj;
        $mail->Body    = $send_body;

        if (!empty($file_attach)) {
            if (is_array($file_attach)) {
                foreach ($file_attach as $attach) {
                    $mail->addAttachment($attach);
                }
            } else {
                $mail->addAttachment($file_attach);
            }
        }

        $mail->IsHTML(true);

        if (!$mail->send()) {
            $CI->app->write_log('Send mail =>' . $send_name . ' - ' . $send_to, $mail->ErrorInfo);
            return "Mailer Error: " . $mail->ErrorInfo;
        } else {
            $CI->app->write_log('Send mail =>' . $send_name . ' to ' . $send_to . json_encode($send_body));
            return "";
        }
    }
}
