use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'backend/PHPMailer/src/Exception.php';
require 'backend/PHPMailer/src/PHPMailer.php';
require 'backend/PHPMailer/src/SMTP.php';
require 'backend/config.php'; // Ensure environment variables are loaded
function sendMail($to, $subject, $body) {
$mail = new PHPMailer(true);
try {
// Enable debugging only for development
$mail->SMTPDebug = 0; // Set to 2 for debugging (1 for commands only)
$mail->isSMTP();
$mail->Host = getenv('SMTP_HOST');
$mail->SMTPAuth = true;
$mail->Username = getenv('SMTP_USER');
$mail->Password = getenv('SMTP_PASS');
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = getenv('SMTP_PORT');
// Sender & Recipient
$mail->setFrom(getenv('SMTP_FROM'), getenv('SMTP_FROM_NAME'));
$mail->addAddress($to);
// Email Content
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
// Attempt to send email
if ($mail->send()) {
return true;
} else {
// Log error but do not break the page
error_log("Mail Error: " . $mail->ErrorInfo);
return false;
}
} catch (Exception $e) {
// Log errors silently
error_log("Mail Exception: " . $mail->ErrorInfo);
return false;
}
}