<?php

/*
 * Copyright (c) 2018 Earworks.
 * Codeigniter Project Template Modification
 * Please contact our email for more information :
 * contact@earworks.co.id
 */

defined('BASEPATH') OR exit('No direct script access allowed');
if (!function_exists('config_mail')) {

    function config_mail($input = array()) {
        $CI = & get_instance();
        $CI->load->library('email');
        $config = array(
            'protocol' => (isset($input['protocol'])) ? $input['protocol'] : 'smtp',
            'smtp_host' => (isset($input['smtp_host'])) ? $input['smtp_host'] : 'smtp.gmail.com',
            'smtp_user' => (isset($input['smtp_user'])) ? $input['smtp_user'] : 'taslin.webdeveloper@gmail.com',
            'smtp_pass' => (isset($input['smtp_pass'])) ? $input['smtp_pass'] : 'jesica12345',
            'smtp_port' => (isset($input['smtp_port'])) ? $input['smtp_port'] : '465',
            'smtp_crypto' => (isset($input['smtp_crypto'])) ? $input['smtp_crypto'] : 'ssl',
            'mailtype' => (isset($input['mailtype'])) ? $input['mailtype'] : 'html',
        );
        $CI->email->initialize($config);
        $CI->email->set_newline("\r\n");
        return $CI;
    }

}
if (!function_exists('send_mail')) {

    /**
     * required parameter on input array :
     * to, subject, message.
     * optional : cc, bcc
     * @param array $input
     * @param array $config
     * @return bool
     */
    function send_mail($input = array(), $config = array()) {
        $CI = config_mail($config);
        /* echo $CI->load->partials('email/layouts/main', array(
          'content' => $input['message'],
          'title' => $input['subject']
          ));
          exit; */
        $CI->email->from('contact@earworks.co.id', 'Earworks');
        $CI->email->to($input['to']);
        if (isset($input['cc'])) {
            foreach (explode($input['cc']) as $cc) {
                $CI->email->cc($cc);
            }
        }
        if (isset($input['bcc'])) {
            foreach (explode($input['bcc']) as $bcc) {
                $CI->email->bcc($bcc);
            }
        }
        $CI->email->subject($input['subject']);
        $CI->email->message($CI->load->partials('email/layouts/main', array(
                    'content' => $input['message'],
                    'title' => $input['subject']
        )));
        return $CI->email->send();
    }

}
