<?php
// ini_set('display_errors', 'On');

// require 'conn/codeblocs.php';
require "packages/vendor/autoload.php";


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

function send_email($sender, $sender_name, $recipient, $recipient_name, $mail_subject, $mail_body, $smtp_id=2){

    $developmentMode = true;

    $mailer = new PHPMailer($developmentMode);


    // EXIT;
    try {

        $smtp_details = SW('*', 'sp_email_smtp_details', 'server_id = ?', 1, 1, [$smtp_id]);
        $user = $smtp_details['smtp_user']; 
        $pwd = $smtp_details['smtp_pwd']; 
        $smtp_server = $smtp_details['server_name']; 
        $smtp_port = $smtp_details['smtp_port']; 
        $smtp_secure = 'tls';
        
        
        $mailer->SMTPDebug = 2;
        $mailer->isSMTP();
        if ($developmentMode) {
            $mailer->SMTPOptions = [
                'ssl'=> [
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                    'allow_self_signed' => true
                ]
            ];
        }
        $mailer->Host = $smtp_server;
        $mailer->SMTPAuth = true;
        $mailer->Username = $user;
        $mailer->Password = $pwd;
        $mailer->SMTPSecure = $smtp_secure;
        $mailer->Port = $smtp_port;
        $mailer->setFrom($sender, $sender_name);
        $mailer->addAddress($recipient, $recipient_name);
        $mailer->isHTML(true);
        $mailer->Subject = $mail_subject;
        $mailer->Body = $mail_body;
        $mailer->send();
        $mailer->ClearAllRecipients();
        echo "MAIL HAS BEEN SENT SUCCESSFULLY";
    } catch (Exception $e) {
        echo "EMAIL SENDING FAILED. INFO: " . $mailer->ErrorInfo;
    }
}