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

use PHPMailer\PHPMailer\PHPMailer;

use PHPMailer\PHPMailer\Exception;

class Mailer {

    protected $_ci;

    // protected $email_pengirim = 'mail@thekecret.masuk.id'; // Isikan dengan email pengirim

    // protected $nama_pengirim = 'Admin'; // Isikan dengan nama pengirim

    // protected $password = '2882edowi92'; // Isikan dengan password email pengirim

    public function __construct(){

        $this->_ci = & get_instance(); // Set variabel _ci dengan Fungsi2-fungsi dari Codeigniter

        require_once(APPPATH.'third_party/PHPMailer-master/src/Exception.php');

        require_once(APPPATH.'third_party/PHPMailer-master/src/PHPMailer.php');

        require_once(APPPATH.'third_party/PHPMailer-master/src/SMTP.php');

    }

    public function send($data){

        // $mail = new PHPMailer;

        // $mail->isSMTP();

        // $mail->Host = $this->_ci->config->item('host');

        // $mail->Username = $this->_ci->config->item('username'); // Email Pengirim

        // $mail->Password = $this->_ci->config->item('password'); // Isikan dengan Password email pengirim

        // $mail->Port = $this->_ci->config->item('port');

        // $mail->SMTPAuth = true;

        // $mail->SMTPSecure = 'tls';

        // // $mail->SMTPDebug = 1; // Aktifkan untuk melakukan debugging

        // $mail->setFrom($this->_ci->config->item('mail-sender'), $this->_ci->config->item('name-sender'));

        // $mail->addAddress($data['email_penerima'], '');

        // // $mail->AddAddress('edowi92@gmail.com', 'Information'); 

        // $mail->isHTML(true); // Aktifkan jika isi emailnya berupa html

        // $mail->Subject = $data['subjek'];

        // $mail->Body = $data['content'];

        // // $mail->AddEmbeddedImage('images/logo.png', 'logo_mynotescode', 'logo.png'); // Aktifkan jika ingin menampilkan gambar dalam email

        // $send = $mail->send();
        $url="http://mailer.gpibcinere.org/index.php";
        $data=array(
            "email_penerima"=>$data['email_penerima'],
            'subjek'=>$data['subjek'],
            'content'=>$data['content'],
            'mail_sender'=>$this->_ci->config->item('mail-sender'),
            'name_sender'=>$this->_ci->config->item('name-sender'),
            'host' => $this->_ci->config->item('host'),
            'username'=>$this->_ci->config->item('username'),
            'password'=>$this->_ci->config->item('password'),
            'port'=>$this->_ci->config->item('port')
        );
    
        // persiapkan curl
        $ch = curl_init(); 
    
        // set url 
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    
        // return the transfer as a string 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    
        // $output contains the output string 
        $output = curl_exec($ch); 
    
        // tutup curl 
        curl_close($ch);  

        if($output){ // Jika Email berhasil dikirim

            $response = array('status'=>'Sukses', 'message'=>'Email berhasil dikirim');

        }else{ // Jika Email Gagal dikirim

            $response = array('status'=>'Gagal', 'message'=>'Email gagal dikirim');

        }

        return $response;







    }

    public function send_with_attachment($data){

        $mail = new PHPMailer;

        $mail->isSMTP();

        $mail->Host = 'mail.thekecret.masuk.id';

        $mail->Username = $this->email_pengirim; // Email Pengirim

        $mail->Password = $this->password; // Isikan dengan Password email pengirim

        $mail->Port = 587;

        $mail->SMTPAuth = true;

        $mail->SMTPSecure = 'tls';

        // $mail->SMTPDebug = 1; // Aktifkan untuk melakukan debugging

        $mail->setFrom($this->email_pengirim, $this->nama_pengirim);

        $mail->addAddress($data['email_penerima'], '');

        $mail->isHTML(true); // Aktifkan jika isi emailnya berupa html

        $mail->Subject = $data['subjek'];

        $mail->Body = $data['content'];

        // $mail->AddEmbeddedImage('image/logo.png', 'logo_mynotescode', 'logo.png'); // Aktifkan jika ingin menampilkan gambar dalam email

        if($data['attachment']['size'] <= 25000000){ // Jika ukuran file <= 25 MB (25.000.000 bytes)

            $mail->addAttachment($data['attachment']['tmp_name'], $data['attachment']['name']);

            $send = $mail->send();

            if($send){ // Jika Email berhasil dikirim

                $response = array('status'=>'Sukses', 'message'=>'Email berhasil dikirim');

            }else{ // Jika Email Gagal dikirim

                $response = array('status'=>'Gagal', 'message'=>'Email gagal dikirim');

            }

        }else{ // Jika Ukuran file lebih dari 25 MB

            $response = array('status'=>'Gagal', 'message'=>'Ukuran file attachment maksimal 25 MB');

        }

        return $response;

    }

}