<?php

namespace App\Controllers;

use App\Models\Md_student;
use App\Models\Md_sms;
use App\Models\Md_cms;

class Mail extends BaseController
{
    
    
    public function __construct() {
         $this->Md_sms= new Md_sms();
         $this->Md_cms= new Md_cms();
         helper('text');
    }
    
 
    public function MailSend()
    {
        
        header("Access-Control-Allow-Origin: *");
        // If you need to allow specific methods
        header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
        // If you need to allow specific headers
        header("Access-Control-Allow-Headers: Content-Type, Authorization");
        $datas = json_decode(file_get_contents("php://input"), true);
      
  
        $subject=$datas['subject'];
        $from_mail=$datas['from_mail'];
        $from_name=$datas['from_name'];
        $to_mail=$datas['to_mail'];
        $body_mail=$datas['body_mail'];
        
        ob_start();
        ini_set('display_errors',1);
        $email = \Config\Services::email();
    
    //   'SMTPHost' => 'smtp.gmail.com',
    //     'SMTPPort' => 587,
    //     'SMTPUser' => 'mvpminfo29@gmail.com',
    //     'SMTPPass' => 'mvpm#1929',
    //     'SMTPCrypto' => 'tls',
    //     'protocol' => 'smtp',
     
    $email->initialize([
        'mailType' => 'html',
        'SMTPAuth' => false,
        'fromEmail' => $from_mail,
        'fromName' => $from_name,
        'subject' => $subject,
    ]);

    $email->setTo($to_mail);
    $email->setMessage($body_mail);
    $response = $email->send();

    // echo '<pre>';
    // print_r(  $email->printDebugger()); 
    // print_r($response);
    // die;
    if($response)
    {
        $final = array('status' => 1,'msg'=>"Email has been sent");
    }else {
        $final = array('status' => 2,'msg'=>$email->printDebugger());
    }
    // $response ? "Email has been sent": $email->printDebugger();

    echo json_encode($final);
    exit();
      
    }
}