<?php

namespace kernel\helpers;

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

class SMTP
{
    public PHPMailer $mail;
    public function __construct()
    {
        $this->mail = new PHPMailer(true);
        $this->mail->CharSet = 'UTF-8';
        $this->mail->isSMTP();
        $this->mail->SMTPAuth = true;
        $this->mail->SMTPDebug = 0;
        $this->mail->Host = $_ENV['MAIL_SMTP_HOST'];
        $this->mail->Port = $_ENV['MAIL_SMTP_PORT'];
        $this->mail->Username = $_ENV['MAIL_SMTP_USERNAME'];
        $this->mail->Password = $_ENV['MAIL_SMTP_PASSWORD'];
    }

    /**
     * @throws Exception
     */
    public function send_html(array $params): bool
    {
        if (!isset($params['address'])){
            return false;
        }
        $this->mail->setFrom($this->mail->Username, $params['from_name'] ?? $this->mail->Host);
        $this->mail->addAddress($params['address']);
        $this->mail->Subject = $params['subject'] ?? 'Без темы';
        $body = $params['body'] ?? 'Нет информации';
        $this->mail->msgHTML($body);

        return $this->mail->send();
    }
}