<?php

namespace App\Helpers;

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


class CMail {
    
    public static function send($config, $reply = false) {
        $mail = new PHPMailer(true);

        try {
            //Server settings
            $mail->SMTPDebug = 2;   
            $mail->Debugoutput = 'error_log';                   //Enable verbose debug output
            $mail->isSMTP();                                            //Send using SMTP
            $mail->Host       = config("services.mail.host");                     //Set the SMTP server to send through
            $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
            $mail->Username   = config("services.mail.username");                     //SMTP username
            $mail->Password   = config("services.mail.password");                               //SMTP password
            $mail->SMTPSecure = config("services.mail.encryption");            //Enable implicit TLS encryption
            $mail->Port       = config("services.mail.port");                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

            //Recipients
            $mail->setFrom(isset($config["from_address"]) ? $config["from_address"] : config("services.mail.from_address"), isset($config["from_name"]) ? $config["from_name"] : config("services.mail.from_name"));
            $mail->addAddress($config["recipient_address"], isset($config["recipient_name"]) ? $config["recipient_name"] : null);     //Add a recipient
            
            if($reply) {
                $mail->addReplyTo(isset($config['replyToAddress']) ? $config['replyToAddress'] : '', isset($config['replyToName']) ? $config['replyToName'] : '');
            }
            //Content
            $mail->isHTML(true);        
            $mail->CharSet  = 'UTF-8';
            $mail->Encoding = 'base64';
            $mail->Subject = $config["subject"];
            $mail->Body    = $config["body"];

            if(!$mail->send()) {
                return false;
            } else {
                return true;
            }
        } catch (Exception $e) {
            logger()->error('Mail error: ' . $mail->ErrorInfo);
    logger()->error('Exception: ' . $e->getMessage());
            return false;
        }
    }



}
?>