<?php
// This file sends a login notification email
// It expects $row and $reg_user to be defined

if (!isset($row) || !isset($reg_user)) {
    // Variables not set, skip email notification
    return;
}

$fname = $row['fname'];
$loginTime = date('Y-m-d H:i:s');
$ipAddress = $_SERVER['REMOTE_ADDR'];

$message = "
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
  <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
  <title>Login Notification</title>
</head>
<body>
  <h2>Dear $fname,</h2>
  <p>We are notifying you that your account has been successfully logged in.</p>
  <p><strong>Login Details:</strong><br>
  Date & Time: $loginTime<br>
  IP Address: $ipAddress</p>
  <p>If this was not you, please secure your account immediately.</p>
  <p>AlphaEdge Online Banking</p>
</body>
</html>
";

$subject = "Login Notification";

// Try to send email, but don't crash if it fails
try {
    if (method_exists($reg_user, 'send_mail')) {
        $reg_user->send_mail($row['email'], $message, $subject);
    }
} catch (Exception $e) {
    // Email failed, but don't stop the login process
    error_log("Login notification email failed: " . $e->getMessage());
}
?>
