<?php
// mail_helper.php (untuk instal manual: PHPMailer ada di pengaturan/PHPMailer/src)
require __DIR__ . '/../Login/config.php'; // tetap sesuaikan

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

require_once __DIR__ . '/PHPMailer/src/Exception.php';
require_once __DIR__ . '/PHPMailer/src/PHPMailer.php';
require_once __DIR__ . '/PHPMailer/src/SMTP.php';

function sendMail($toEmail, $subject, $htmlBody) {
    $smtpHost = defined('SMTP_HOST') ? SMTP_HOST : 'smtp.gmail.com';
    $smtpUser = defined('SMTP_USER') ? SMTP_USER : 'sempurane2023@gmail.com';
    $smtpPass = defined('SMTP_PASS') ? SMTP_PASS : 'vqdyjdwcmgvepzmc';
    $smtpPort = defined('SMTP_PORT') ? SMTP_PORT : 587;
    $smtpSecure = defined('SMTP_SECURE') ? SMTP_SECURE : 'tls';

    $mail = new PHPMailer(true);
    try {
        $mail->isSMTP();
        $mail->Host       = $smtpHost;
        $mail->SMTPAuth   = true;
        $mail->Username   = $smtpUser;
        $mail->Password   = $smtpPass;
        $mail->SMTPSecure = $smtpSecure;
        $mail->Port       = $smtpPort;

        $mail->setFrom($smtpUser, 'Pustakaia');
        $mail->addAddress($toEmail);
        $mail->isHTML(true);
        $mail->Subject = $subject;
        $mail->Body    = $htmlBody;
        $mail->AltBody = strip_tags($htmlBody);

        $mail->send();
        return true;
    } catch (Exception $e) {
        return "Mailer Error: " . $mail->ErrorInfo;
    }
}
