Sindbad~EG File Manager
<?php
require_once '../config/config.php';
// Check if user is logged in and has proper permissions
if (!isLoggedIn()) {
redirect('login.php');
}
// Only admin and superusers can access email management
if (!hasRole('admin') && !hasRole('superuser')) {
redirect('dashboard.php');
}
$db = new Database();
$conn = $db->getConnection();
$success_message = '';
$error_message = '';
// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!validateCSRFToken($_POST['csrf_token'] ?? '')) {
$error_message = 'Invalid security token. Please try again.';
} else {
$action = $_POST['action'] ?? '';
if ($action === 'update_settings') {
$smtp_host = sanitizeInput($_POST['smtp_host'] ?? '');
$smtp_port = (int)($_POST['smtp_port'] ?? 587);
$smtp_username = sanitizeInput($_POST['smtp_username'] ?? '');
$smtp_password = $_POST['smtp_password'] ?? ''; // Don't sanitize password
$smtp_encryption = sanitizeInput($_POST['smtp_encryption'] ?? 'tls');
$from_email = sanitizeInput($_POST['from_email'] ?? '');
$from_name = sanitizeInput($_POST['from_name'] ?? '');
$is_active = isset($_POST['is_active']) ? 1 : 0;
if (empty($smtp_host) || empty($smtp_username) || empty($from_email) || empty($from_name)) {
$error_message = 'Please fill in all required fields.';
} else {
try {
// Check if settings exist
$check_query = "SELECT COUNT(*) FROM email_settings";
$check_stmt = $conn->prepare($check_query);
$check_stmt->execute();
$exists = $check_stmt->fetchColumn() > 0;
if ($exists) {
// Update existing settings
$query = "UPDATE email_settings SET
smtp_host = ?, smtp_port = ?, smtp_username = ?,
smtp_password = ?, smtp_encryption = ?, from_email = ?,
from_name = ?, is_active = ?, updated_at = NOW()";
$stmt = $conn->prepare($query);
$stmt->execute([$smtp_host, $smtp_port, $smtp_username, $smtp_password,
$smtp_encryption, $from_email, $from_name, $is_active]);
} else {
// Insert new settings
$query = "INSERT INTO email_settings (smtp_host, smtp_port, smtp_username, smtp_password,
smtp_encryption, from_email, from_name, is_active)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = $conn->prepare($query);
$stmt->execute([$smtp_host, $smtp_port, $smtp_username, $smtp_password,
$smtp_encryption, $from_email, $from_name, $is_active]);
}
logActivity($_SESSION['user_id'], 'update_email_settings', 'Updated email configuration');
$success_message = 'Email settings updated successfully.';
} catch (Exception $e) {
$error_message = 'An error occurred while updating email settings.';
}
}
} elseif ($action === 'update_template') {
$template_id = (int)($_POST['template_id'] ?? 0);
$subject = sanitizeInput($_POST['subject'] ?? '');
$body = $_POST['body'] ?? ''; // Don't sanitize HTML content
$is_active = isset($_POST['template_active']) ? 1 : 0;
if (empty($subject) || empty($body)) {
$error_message = 'Subject and body are required for email template.';
} else {
try {
$query = "UPDATE email_templates SET subject = ?, body = ?, is_active = ?, updated_at = NOW() WHERE id = ?";
$stmt = $conn->prepare($query);
$stmt->execute([$subject, $body, $is_active, $template_id]);
logActivity($_SESSION['user_id'], 'update_email_template', "Updated email template ID: $template_id");
$success_message = 'Email template updated successfully.';
} catch (Exception $e) {
$error_message = 'An error occurred while updating email template.';
}
}
} elseif ($action === 'test_email') {
$test_email = sanitizeInput($_POST['test_email'] ?? '');
if (empty($test_email) || !filter_var($test_email, FILTER_VALIDATE_EMAIL)) {
$error_message = 'Please enter a valid email address for testing.';
} else {
require_once '../includes/email_functions_improved.php';
$result = testEmailConfiguration($conn, $test_email);
if ($result) {
$success_message = "Test email sent successfully to $test_email";
} else {
$error_message = "Test email failed to send to $test_email. Check Email Diagnostics for details.";
}
}
}
}
}
// Get current email settings
$settings_query = "SELECT * FROM email_settings ORDER BY created_at DESC LIMIT 1";
$settings_stmt = $conn->prepare($settings_query);
$settings_stmt->execute();
$email_settings = $settings_stmt->fetch();
// Get email templates
$templates_query = "SELECT * FROM email_templates ORDER BY template_name";
$templates_stmt = $conn->prepare($templates_query);
$templates_stmt->execute();
$email_templates = $templates_stmt->fetchAll();
// Get site settings
$query = "SELECT setting_key, setting_value FROM settings WHERE setting_key IN ('site_title', 'site_logo')";
$stmt = $conn->prepare($query);
$stmt->execute();
$site_settings = [];
while ($row = $stmt->fetch()) {
$site_settings[$row['setting_key']] = $row['setting_value'];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Email Management - <?php echo $site_settings['site_title'] ?? SITE_TITLE; ?></title>
<script src="https://cdn.tailwindcss.com"></script>
<script>
tailwind.config = {
theme: {
extend: {
colors: {
primary: '#3B82F6',
secondary: '#F59E0B',
accent: '#6B7280'
}
}
}
}
</script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
<style>
.gradient-bg {
background: linear-gradient(135deg, #3B82F6 0%, #F59E0B 50%, #6B7280 100%);
}
</style>
</head>
<body class="bg-gray-50">
<!-- Include Sidebar -->
<?php include 'includes/sidebar.php'; ?>
<!-- Main Content -->
<div class="md:ml-64">
<!-- Header -->
<header class="bg-white shadow-sm border-b">
<div class="px-6 py-4">
<div class="flex items-center justify-between">
<h1 class="text-2xl font-bold text-gray-900">Email Management</h1>
<div class="space-x-2">
<a href="test_smtp.php" class="bg-green-600 text-white px-4 py-2 rounded-lg hover:bg-green-700 transition duration-300 text-sm">
<i class="fas fa-paper-plane mr-2"></i>Test SMTP
</a>
<a href="email_setup_guide.php" class="bg-blue-600 text-white px-4 py-2 rounded-lg hover:bg-blue-700 transition duration-300 text-sm">
<i class="fas fa-question-circle mr-2"></i>Setup Guide
</a>
</div>
</div>
</div>
</header>
<!-- Content -->
<main class="p-6">
<!-- Success/Error Messages -->
<?php if ($success_message): ?>
<div class="bg-green-50 border border-green-200 text-green-700 px-4 py-3 rounded-lg mb-6">
<i class="fas fa-check-circle mr-2"></i>
<?php echo $success_message; ?>
</div>
<?php endif; ?>
<?php if ($error_message): ?>
<div class="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-lg mb-6">
<i class="fas fa-exclamation-triangle mr-2"></i>
<?php echo $error_message; ?>
</div>
<?php endif; ?>
<!-- Email Settings -->
<div class="bg-white rounded-lg shadow mb-8">
<div class="px-6 py-4 border-b border-gray-200">
<h2 class="text-xl font-semibold text-gray-900">
<i class="fas fa-cog mr-2 text-primary"></i>SMTP Configuration
</h2>
</div>
<div class="p-6">
<form method="POST">
<input type="hidden" name="csrf_token" value="<?php echo generateCSRFToken(); ?>">
<input type="hidden" name="action" value="update_settings">
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<div>
<label for="smtp_host" class="block text-sm font-medium text-gray-700 mb-2">SMTP Host *</label>
<input type="text" id="smtp_host" name="smtp_host"
value="<?php echo htmlspecialchars($email_settings['smtp_host'] ?? ''); ?>"
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-transparent"
placeholder="smtp.gmail.com" required>
</div>
<div>
<label for="smtp_port" class="block text-sm font-medium text-gray-700 mb-2">SMTP Port *</label>
<input type="number" id="smtp_port" name="smtp_port"
value="<?php echo $email_settings['smtp_port'] ?? 587; ?>"
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-transparent"
required>
</div>
<div>
<label for="smtp_username" class="block text-sm font-medium text-gray-700 mb-2">SMTP Username *</label>
<input type="text" id="smtp_username" name="smtp_username"
value="<?php echo htmlspecialchars($email_settings['smtp_username'] ?? ''); ?>"
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-transparent"
placeholder="your-email@gmail.com" required>
</div>
<div>
<label for="smtp_password" class="block text-sm font-medium text-gray-700 mb-2">SMTP Password *</label>
<input type="password" id="smtp_password" name="smtp_password"
value="<?php echo htmlspecialchars($email_settings['smtp_password'] ?? ''); ?>"
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-transparent"
placeholder="App Password or Account Password" required>
</div>
<div>
<label for="smtp_encryption" class="block text-sm font-medium text-gray-700 mb-2">Encryption</label>
<select id="smtp_encryption" name="smtp_encryption"
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-transparent">
<option value="tls" <?php echo ($email_settings['smtp_encryption'] ?? 'tls') === 'tls' ? 'selected' : ''; ?>>TLS</option>
<option value="ssl" <?php echo ($email_settings['smtp_encryption'] ?? '') === 'ssl' ? 'selected' : ''; ?>>SSL</option>
<option value="none" <?php echo ($email_settings['smtp_encryption'] ?? '') === 'none' ? 'selected' : ''; ?>>None</option>
</select>
</div>
<div>
<label for="from_email" class="block text-sm font-medium text-gray-700 mb-2">From Email *</label>
<input type="email" id="from_email" name="from_email"
value="<?php echo htmlspecialchars($email_settings['from_email'] ?? ''); ?>"
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-transparent"
required>
</div>
<div>
<label for="from_name" class="block text-sm font-medium text-gray-700 mb-2">From Name *</label>
<input type="text" id="from_name" name="from_name"
value="<?php echo htmlspecialchars($email_settings['from_name'] ?? ''); ?>"
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-transparent"
placeholder="Church Attendance System" required>
</div>
<div class="md:col-span-2">
<label class="flex items-center">
<input type="checkbox" name="is_active" value="1"
<?php echo ($email_settings['is_active'] ?? 1) ? 'checked' : ''; ?>
class="rounded border-gray-300 text-primary focus:ring-primary">
<span class="ml-2 text-sm text-gray-700">Enable email notifications</span>
</label>
</div>
</div>
<div class="mt-6 flex space-x-4">
<button type="submit" class="bg-primary text-white px-6 py-2 rounded-lg hover:bg-blue-700 transition duration-300">
<i class="fas fa-save mr-2"></i>Save Settings
</button>
</div>
</form>
</div>
</div>
<!-- Test Email -->
<div class="bg-white rounded-lg shadow mb-8">
<div class="px-6 py-4 border-b border-gray-200">
<h2 class="text-xl font-semibold text-gray-900">
<i class="fas fa-paper-plane mr-2 text-primary"></i>Test Email
</h2>
</div>
<div class="p-6">
<form method="POST">
<input type="hidden" name="csrf_token" value="<?php echo generateCSRFToken(); ?>">
<input type="hidden" name="action" value="test_email">
<div class="max-w-md">
<label for="test_email" class="block text-sm font-medium text-gray-700 mb-2">Test Email Address</label>
<div class="flex">
<input type="email" id="test_email" name="test_email"
class="flex-1 px-3 py-2 border border-gray-300 rounded-l-lg focus:ring-2 focus:ring-primary focus:border-transparent"
placeholder="test@example.com" required>
<button type="submit" class="bg-secondary text-white px-4 py-2 rounded-r-lg hover:bg-yellow-600 transition duration-300">
<i class="fas fa-paper-plane mr-2"></i>Send Test
</button>
</div>
</div>
</form>
</div>
</div>
<!-- Email Templates -->
<div class="bg-white rounded-lg shadow">
<div class="px-6 py-4 border-b border-gray-200">
<h2 class="text-xl font-semibold text-gray-900">
<i class="fas fa-envelope mr-2 text-primary"></i>Email Templates
</h2>
</div>
<div class="p-6">
<?php foreach ($email_templates as $template): ?>
<div class="border border-gray-200 rounded-lg p-6 mb-6">
<form method="POST">
<input type="hidden" name="csrf_token" value="<?php echo generateCSRFToken(); ?>">
<input type="hidden" name="action" value="update_template">
<input type="hidden" name="template_id" value="<?php echo $template['id']; ?>">
<div class="mb-4">
<h3 class="text-lg font-semibold text-gray-900 mb-2">
<?php echo ucwords(str_replace('_', ' ', $template['template_name'])); ?>
</h3>
</div>
<div class="mb-4">
<label for="subject_<?php echo $template['id']; ?>" class="block text-sm font-medium text-gray-700 mb-2">Subject</label>
<input type="text" id="subject_<?php echo $template['id']; ?>" name="subject"
value="<?php echo htmlspecialchars($template['subject']); ?>"
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-transparent"
required>
</div>
<div class="mb-4">
<label for="body_<?php echo $template['id']; ?>" class="block text-sm font-medium text-gray-700 mb-2">Email Body</label>
<textarea id="body_<?php echo $template['id']; ?>" name="body" rows="10"
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-transparent"
required><?php echo htmlspecialchars($template['body']); ?></textarea>
<p class="text-sm text-gray-500 mt-2">
Available variables: {{full_name}}, {{program_name}}, {{tracking_code}}, {{district_name}}, {{assembly_name}}, {{submission_date}}
</p>
</div>
<div class="mb-4">
<label class="flex items-center">
<input type="checkbox" name="template_active" value="1"
<?php echo $template['is_active'] ? 'checked' : ''; ?>
class="rounded border-gray-300 text-primary focus:ring-primary">
<span class="ml-2 text-sm text-gray-700">Active template</span>
</label>
</div>
<button type="submit" class="bg-primary text-white px-4 py-2 rounded-lg hover:bg-blue-700 transition duration-300">
<i class="fas fa-save mr-2"></i>Update Template
</button>
</form>
</div>
<?php endforeach; ?>
</div>
</div>
</main>
</div>
</body>
</html>
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists