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

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

// Adjust paths to your PHPMailer installation
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);

        // Server settings (these are fixed and reusable)
        $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 = 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(); // Clear previous recipients
            $this->mail->addAddress($toEmail, $toName);
            $this->mail->addCC('azizulhoqueakash@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;
        }
    }
}