<?php

namespace kernel\helpers;

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

class SMTP
{
    public PHPMailer $mail;
    public string $host;
    public int $port;
    public string $username;
    public string $password;

    public function __construct(array $config = [])
    {
        $this->mail = new PHPMailer(true);
        $this->mail->isSMTP();
        $this->mail->SMTPAuth = true;
        $this->mail->SMTPDebug = 0;
        $this->host = $config['host'] ?? '';
        $this->port = $config['port'] ?? 587;
        $this->username = $config['username'] ?? '';
        $this->password = $config['password'] ?? '';
    }

    /**
     * @throws Exception
     */
    public function send(string $address): void
    {
        $this->mail->setFrom($this->username, $this->host);
        $this->mail->addAddress($address);
        $this->mail->Subject = 'Код подтверждения';
        $body = '<p><strong>«Hello, world!» </strong></p>';
        $this->mail->msgHTML($body);

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