<?php
/**
* PHP Mailer Configuration
* This file contains the configuration for the PHP Mailer library
* Used for email verification and contact form
*/
// Path to PHPMailer if installed via Composer
if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
} else {
// Manual inclusion of PHPMailer files if Composer is not used
require_once __DIR__ . '/../libs/PHPMailer/src/Exception.php';
require_once __DIR__ . '/../libs/PHPMailer/src/PHPMailer.php';
require_once __DIR__ . '/../libs/PHPMailer/src/SMTP.php';
}
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
/**
* Initialize PHPMailer with default configuration
*
* @return PHPMailer Configured PHPMailer instance
*/
function initMailer() {
$mail = new PHPMailer(true);
// Server settings
$mail->isSMTP();
$mail->Mailer = 'smtp';
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'xscqqwncdsefbdnl'; // App password without spaces
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
// Set TLS auto to false to avoid deprecation warnings
$mail->SMTPAutoTLS = false;
return $mail;
}
/**
* Send a verification email to the user
*
* @param string $to_email Email address of the recipient
* @param string $to_name Name of the recipient
* @param string $token Verification token
* @param int $user_id User ID for verification
* @return bool True if email was sent successfully, false otherwise
*/
function sendVerificationEmail($to_email, $to_name, $token, $user_id) {
$mail = initMailer();
if (!$mail) {
return false;
}
try {
// Set recipient
$mail->addAddress($to_email, $to_name);
// Email subject
$mail->Subject = 'Verify Your Email Address - Popular Computer Institute';
// Generate verification link
$verification_link = 'https://' . $_SERVER['HTTP_HOST'] . '/verify_email.php?token=' . $token . '&id=' . $user_id;
// Email body in HTML format
$mail->isHTML(true);
$mail->Body = '
<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
color: #333;
}
.container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ddd;
border-radius: 5px;
}
.header {
background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
padding: 20px;
text-align: center;
color: white;
border-radius: 5px 5px 0 0;
}
.content {
padding: 20px;
}
.button {
display: inline-block;
padding: 10px 20px;
background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
color: white;
text-decoration: none;
border-radius: 5px;
font-weight: bold;
margin: 20px 0;
}
.footer {
margin-top: 30px;
text-align: center;
font-size: 12px;
color: #777;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>Email Verification</h1>
</div>
<div class="content">
<h2>Hello ' . htmlspecialchars($to_name) . ',</h2>
<p>Thank you for registering with Popular Computer Institute. Please click the button below to verify your email address:</p>
<div style="text-align: center;">
<a href="' . $verification_link . '" class="button">Verify Email Address</a>
</div>
<p>If the button above doesn\'t work, please copy and paste the following link into your browser:</p>
<p style="word-break: break-all;">' . $verification_link . '</p>
<p>This link will expire in 24 hours.</p>
<p>If you didn\'t create an account with us, please ignore this email.</p>
</div>
<div class="footer">
<p>© ' . date('Y') . ' Popular Computer Institute. All rights reserved.</p>
</div>
</div>
</body>
</html>';
// Plain text version for non-HTML email clients
$mail->AltBody = 'Hello ' . $to_name . ',
Thank you for registering with Popular Computer Institute. Please visit the following link to verify your email address:
' . $verification_link . '
This link will expire in 24 hours. If you didn\'t create an account with us, please ignore this email.
Popular Computer Institute';
// Send the email
$mail->send();
return true;
} catch (Exception $e) {
error_log("Failed to send verification email to {$to_email}: " . $e->getMessage());
return false;
}
}
/**
* Send a contact form submission email
*
* @param string $name Name of the person submitting the form
* @param string $email Email of the person submitting the form
* @param string $subject Subject of the message
* @param string $message Content of the message
* @param string $phone Optional phone number
* @param bool $send_autoreply Whether to send an auto-reply to the submitter
* @return bool True if email was sent successfully, false otherwise
*/
function sendContactFormEmail($name, $email, $subject, $message, $phone = '', $send_autoreply = true) {
try {
$mail = initMailer();
// Sender and recipient
$mail->setFrom('[email protected]', 'Popular Computer Institute');
$mail->addReplyTo($email, $name);
$mail->addAddress('[email protected]', 'Popular Computer Institute');
// Email content
$mail->isHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Subject = 'Contact Form: ' . $subject;
// Email body
$html_message = "
<html>
<head>
<style>
body { font-family: Arial, sans-serif; line-height: 1.6; }
.container { max-width: 600px; margin: 0 auto; border: 1px solid #ddd; padding: 20px; }
.header { background-color: #4a6fdc; padding: 10px; color: white; text-align: center; }
.content { padding: 20px; }
.footer { font-size: 12px; color: #777; border-top: 1px solid #eee; margin-top: 20px; padding-top: 10px; }
</style>
</head>
<body>
<div class='container'>
<div class='header'>
<h2>New Contact Form Message</h2>
</div>
<div class='content'>
<p><strong>Name:</strong> {$name}</p>
<p><strong>Email:</strong> {$email}</p>";
// Add phone if provided
if (!empty($phone)) {
$html_message .= "<p><strong>Phone:</strong> {$phone}</p>";
}
$html_message .= "
<p><strong>Subject:</strong> {$subject}</p>
<p><strong>Message:</strong></p>
<p>" . nl2br($message) . "</p>
</div>
<div class='footer'>
<p>This message was sent from the contact form on your website.</p>
<p>IP: " . ($_SERVER['REMOTE_ADDR'] ?? 'Unknown') . "</p>
<p>Date: " . date('Y-m-d H:i:s') . "</p>
</div>
</div>
</body>
</html>";
$mail->Body = $html_message;
$mail->AltBody = "Contact Form Message\n\nName: {$name}\nEmail: {$email}" .
(!empty($phone) ? "\nPhone: {$phone}" : "") .
"\nSubject: {$subject}\n\nMessage:\n{$message}\n\n" .
"Sent from: " . ($_SERVER['REMOTE_ADDR'] ?? 'Unknown') . " on " . date('Y-m-d H:i:s');
return $mail->send();
} catch (Exception $e) {
error_log("Error sending contact form email: " . $e->getMessage());
return false;
}
}
/**
* Send a password reset email
*
* @param string $to_email Email address of the recipient
* @param string $to_name Name of the recipient
* @param string $token Reset token
* @param int $user_id User ID for the reset link
* @return bool True if email was sent successfully, false otherwise
*/
function sendPasswordResetEmail($to_email, $to_name, $token, $user_id) {
$mail = initMailer();
if (!$mail) {
return false;
}
try {
// Set recipient
$mail->addAddress($to_email, $to_name);
// Email subject
$mail->Subject = 'Password Reset - Popular Computer Institute';
// Generate reset link
$reset_link = 'https://' . $_SERVER['HTTP_HOST'] . '/reset_password.php?token=' . $token . '&id=' . $user_id;
// Email body in HTML format
$mail->isHTML(true);
$mail->Body = '
<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
color: #333;
}
.container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ddd;
border-radius: 5px;
}
.header {
background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
padding: 20px;
text-align: center;
color: white;
border-radius: 5px 5px 0 0;
}
.content {
padding: 20px;
}
.button {
display: inline-block;
padding: 10px 20px;
background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
color: white;
text-decoration: none;
border-radius: 5px;
font-weight: bold;
margin: 20px 0;
}
.footer {
margin-top: 30px;
text-align: center;
font-size: 12px;
color: #777;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>Password Reset</h1>
</div>
<div class="content">
<h2>Hello ' . htmlspecialchars($to_name) . ',</h2>
<p>We received a request to reset your password. Please click the button below to set a new password:</p>
<div style="text-align: center;">
<a href="' . $reset_link . '" class="button">Reset Password</a>
</div>
<p>If the button above doesn\'t work, please copy and paste the following link into your browser:</p>
<p style="word-break: break-all;">' . $reset_link . '</p>
<p>This link will expire in 1 hour.</p>
<p>If you didn\'t request a password reset, please ignore this email. Your password will remain unchanged.</p>
</div>
<div class="footer">
<p>© ' . date('Y') . ' Popular Computer Institute. All rights reserved.</p>
</div>
</div>
</body>
</html>';
// Plain text version for non-HTML email clients
$mail->AltBody = 'Hello ' . $to_name . ',
We received a request to reset your password. Please visit the following link to set a new password:
' . $reset_link . '
This link will expire in 1 hour. If you didn\'t request a password reset, please ignore this email. Your password will remain unchanged.
Popular Computer Institute';
// Send the email
$mail->send();
return true;
} catch (Exception $e) {
error_log("Failed to send password reset email to {$to_email}: " . $e->getMessage());
return false;
}
}