<?php
// Load PHPMailer classes
require_once __DIR__ . '/PHPMailer/Exception.php';
require_once __DIR__ . '/PHPMailer/PHPMailer.php';
require_once __DIR__ . '/PHPMailer/SMTP.php';

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

function sendEmail($to, $subject, $body, $altBody = '') {
    // Create a new PHPMailer instance
    $mail = new PHPMailer(true);
    
    try {
        // Server settings - Configured for noreply@bmyouth.org
        $mail->isSMTP();                                      // Send using SMTP
        $mail->Host       = 'smtp.gmail.com';                 // SMTP server (using Gmail SMTP)
        $mail->SMTPAuth   = true;                             // Enable SMTP authentication
        $mail->Username   = 'noreply@bmyouth.org';            // SMTP username
        $mail->Password   = '@Anupsinha@321';                 // SMTP password
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;      // Enable implicit TLS encryption
        $mail->Port       = 465;                              // TCP port to connect to
        
        // Optional: Enable verbose debug output (remove in production)
        // $mail->SMTPDebug = SMTP::DEBUG_SERVER;
        
        // Sender & Recipients
        $mail->setFrom('noreply@bmyouth.org', 'BMYA-SHELDANG');
        $mail->addAddress($to);                               // Add a recipient
        
        // Content
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = $subject;
        $mail->Body    = $body;
        $mail->AltBody = $altBody ?: strip_tags($body);       // Plain text alternative
        
        $mail->send();
        return ['success' => true, 'message' => 'Email sent successfully'];
    } catch (Exception $e) {
        return ['success' => false, 'message' => "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"];
    }
}

// Test function to check if email configuration is working
function testEmailConfiguration($testEmail) {
    $subject = "Test Email from BMYA-SHELDANG";
    $body = "
    <html>
    <head>
        <title>Test Email</title>
    </head>
    <body>
        <div style='font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px;'>
            <div style='background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); padding: 20px; text-align: center; color: white; border-radius: 10px;'>
                <h2>BMYA-SHELDANG</h2>
                <p>Email Configuration Test</p>
            </div>
            <div style='background: #f5f5f5; padding: 20px; border-radius: 10px; margin-top: 10px;'>
                <p>Congratulations! Your email configuration is working correctly.</p>
                <p>This is a test email sent from your BMYA-SHELDANG system.</p>
                <p>Time: " . date('Y-m-d H:i:s') . "</p>
            </div>
        </div>
    </body>
    </html>
    ";
    
    return sendEmail($testEmail, $subject, $body);
}
?>