<?php
require '../config/config.php';

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

require '../vendor/PHPMailer/src/Exception.php';
require '../vendor/PHPMailer/src/PHPMailer.php';
require '../vendor/PHPMailer/src/SMTP.php';

class Mailer
{

    private $mail;

    public function __construct()
    {
        $this->mail = new PHPMailer(true);

        $this->mail->isSMTP();
        $this->mail->Host = 'snapmomentbd.com';
        $this->mail->SMTPAuth = true;
        $this->mail->Username = 'info@snapmomentbd.com';
        $this->mail->Password = '*3#HxP@T9$!X';
        // $this->mail->SMTPSecure = 'ssl'; // 'ssl' || PHPMailer::ENCRYPTION_SMTPS
        // $this->mail->Port = 465;
        $this->mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //'tls'
        $this->mail->Port = 587;
        $this->mail->setFrom('info@snapmomentbd.com', 'Snap Moment');
    }

    /**
     * Sends an email with a dynamic HTML template.
     *
     * @param string $toEmail The recipient's email address.
     * @param string $toName The recipient's name.
     * @param string $subject The email subject.
     * @param string $htmlBody The HTML content of the email.
     * @param string $altBody The plain-text content for the email.
     * @return bool True on success, false on failure.
     */
    public function SendEmail($toEmail, $toName, $subject, $htmlBody)
    {
        try {
            $this->mail->clearAddresses();
            $this->mail->addAddress($toEmail, $toName);
            $this->mail->addCC('snapmomentbd@gmail.com');
            $this->mail->addCC('info@snapmomenbd.com');
            $this->mail->addBCC('azizulhoqueakash@gmail.com');
            $this->mail->addBCC('mahmudamim24@gmail.com');
            $this->mail->addBCC('mdjunayedji@gmail.com');
            $this->mail->addReplyTo('snapmomentbd@gmail.com');
            $this->mail->isHTML(true);
            $this->mail->Subject = $subject . ' - Snap Moment';
            $this->mail->Body = $htmlBody;
            $this->mail->AltBody = strip_tags($htmlBody);

            return $this->mail->send();
        } catch (Exception $e) {
            error_log("Error: " . $this->mail->ErrorInfo);
            return false;
        }
    }
}