| Server IP : 127.0.0.1 / Your IP : 192.168.99.1 Web Server : Apache/2.4.35 (Win64) OpenSSL/1.1.1b mod_fcgid/2.3.9 System : Windows NT WIN-JEJMB9EN0L0 10.0 build 14393 (Windows Server 2016) AMD64 User : Administrator ( 0) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : D:/bpkd_/admin/ |
Upload File : |
<?php
$page_title = 'Kotak Masuk Email';
require_once 'config/database.php';
require_once '../includes/config.php';
require_once 'includes/init.php';
requireLogin();
// Hanya admin yang bisa mengakses
if (!in_array($_SESSION['role'] , ['superadmin','admn'])) {
redirect(BASE_URL . '/admin/dashboard.php');
}
// Load PHPMailer jika menggunakan Composer
// Ganti dengan ini (manual include)
require_once '../vendor/phpmailer/src/PHPMailer.php';
require_once '../vendor/phpmailer/src/SMTP.php';
require_once '../vendor/phpmailer/src/Exception.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
// =====================================================
// KONFIGURASI EMAIL
// =====================================================
$smtp_host = $settings['smtp_host'] ?? 'smtp.gmail.com';
$smtp_port = $settings['smtp_port'] ?? '587';
$smtp_username = $settings['smtp_username'] ?? '';
$smtp_password = $settings['smtp_password'] ?? '';
$smtp_encryption = $settings['smtp_encryption'] ?? 'tls';
$imap_host = $settings['imap_host'] ?? '{imap.gmail.com:993/imap/ssl}INBOX';
$email_inbox_active = $settings['email_inbox_active'] ?? '0';
// =====================================================
// CEK KONEKSI IMAP
// =====================================================
function checkImapConnection($host, $username, $password) {
if (empty($username) || empty($password)) {
return false;
}
$imap = @imap_open($host, $username, $password);
if ($imap) {
imap_close($imap);
return true;
}
return false;
}
$imap_connected = checkImapConnection($imap_host, $smtp_username, $smtp_password);
// =====================================================
// HANDLE SEND REPLY
// =====================================================
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['send_reply'])) {
$to = $_POST['to_email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$original_id = (int)$_POST['original_id'];
if (!empty($to) && !empty($subject) && !empty($message)) {
$mail = new PHPMailer(true);
try {
// Server settings
$mail->isSMTP();
$mail->Host = $smtp_host;
$mail->SMTPAuth = true;
$mail->Username = $smtp_username;
$mail->Password = $smtp_password;
$mail->SMTPSecure = $smtp_encryption;
$mail->Port = $smtp_port;
$mail->CharSet = 'UTF-8';
// Recipients
$mail->setFrom($smtp_username, $settings['site_title'] ?? 'Admin');
$mail->addAddress($to);
$mail->addReplyTo($smtp_username);
// Content
$mail->isHTML(true);
$mail->Subject = 'Re: ' . $subject;
$mail->Body = nl2br($message);
$mail->AltBody = strip_tags($message);
$mail->send();
// Simpan ke log
$conn->query("INSERT INTO email_log (original_id, to_email, subject, message, status, sent_at)
VALUES ($original_id, '$to', '$subject', '$message', 'sent', NOW())");
$success = "Balasan email berhasil dikirim!";
} catch (Exception $e) {
$error = "Email gagal dikirim. Mailer Error: {$mail->ErrorInfo}";
}
} else {
$error = "Semua field harus diisi!";
}
}
// =====================================================
// BACA EMAIL DARI IMAP
// =====================================================
$emails = [];
$error_imap = '';
if ($imap_connected && $email_inbox_active == '1') {
$imap = @imap_open($imap_host, $smtp_username, $smtp_password);
if ($imap) {
// Get total emails
$total = imap_num_msg($imap);
$limit = 20; // Batasi 20 email terbaru
$start = max(1, $total - $limit + 1);
for ($i = $total; $i >= $start; $i--) {
$header = imap_headerinfo($imap, $i);
$body = imap_fetchbody($imap, $i, 1);
// Decode body
if (imap_fetchstructure($imap, $i)->encoding == 3) {
$body = imap_base64($body);
} elseif (imap_fetchstructure($imap, $i)->encoding == 4) {
$body = imap_qprint($body);
}
// Clean body
$body = strip_tags($body);
$body = trim(preg_replace('/\s+/', ' ', $body));
$emails[] = [
'id' => $i,
'from' => $header->from[0]->mailbox . '@' . $header->from[0]->host,
'from_name' => $header->from[0]->personal ?? $header->from[0]->mailbox,
'subject' => $header->subject ?? '(No Subject)',
'date' => date('Y-m-d H:i:s', strtotime($header->date)),
'body' => substr($body, 0, 500),
'body_full' => $body,
'seen' => $header->Unseen == 'U' ? 0 : 1
];
}
// Update last check time
$conn->query("UPDATE settings SET setting_value = NOW() WHERE setting_key = 'email_last_check'");
imap_close($imap);
} else {
$error_imap = "Tidak dapat terhubung ke server email. Periksa pengaturan IMAP.";
}
}
// Include header
require_once 'includes/header.php';
?>
<div class="container-fluid">
<div class="d-sm-flex align-items-center justify-content-between mb-4">
<h1 class="h3 mb-0 text-gray-800">
<i class="fas fa-envelope me-2"></i>Kotak Masuk Email
</h1>
<div>
<button class="btn btn-info me-2" onclick="refreshInbox()">
<i class="fas fa-sync-alt me-2"></i>Refresh
</button>
<a href="email_settings.php" class="btn btn-primary">
<i class="fas fa-cog me-2"></i>Pengaturan Email
</a>
</div>
</div>
<!-- Status Card -->
<div class="row mb-4">
<div class="col-md-4">
<div class="card shadow border-left-primary">
<div class="card-body">
<div class="row no-gutters align-items-center">
<div class="col mr-2">
<div class="text-xs font-weight-bold text-primary text-uppercase mb-1">
Status Koneksi IMAP
</div>
<div class="h5 mb-0 font-weight-bold text-gray-800">
<?php if ($imap_connected): ?>
<span class="badge bg-success">Terkoneksi</span>
<?php else: ?>
<span class="badge bg-danger">Tidak Terkoneksi</span>
<br>
<small class="text-muted">Periksa pengaturan email</small>
<?php endif; ?>
</div>
</div>
<div class="col-auto">
<i class="fas fa-plug fa-2x text-gray-300"></i>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card shadow border-left-success">
<div class="card-body">
<div class="row no-gutters align-items-center">
<div class="col mr-2">
<div class="text-xs font-weight-bold text-success text-uppercase mb-1">
Total Email
</div>
<div class="h5 mb-0 font-weight-bold text-gray-800">
<?php echo count($emails); ?> (terbaru)
</div>
</div>
<div class="col-auto">
<i class="fas fa-envelope fa-2x text-gray-300"></i>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card shadow border-left-warning">
<div class="card-body">
<div class="row no-gutters align-items-center">
<div class="col mr-2">
<div class="text-xs font-weight-bold text-warning text-uppercase mb-1">
Email Belum Dibaca
</div>
<div class="h5 mb-0 font-weight-bold text-gray-800">
<?php
$unread = count(array_filter($emails, function($e) { return $e['seen'] == 0; }));
echo $unread;
?>
</div>
</div>
<div class="col-auto">
<i class="fas fa-eye fa-2x text-gray-300"></i>
</div>
</div>
</div>
</div>
</div>
</div>
<?php if (isset($success)): ?>
<div class="alert alert-success alert-dismissible fade show">
<i class="fas fa-check-circle me-2"></i><?php echo $success; ?>
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<?php if (isset($error)): ?>
<div class="alert alert-danger alert-dismissible fade show">
<i class="fas fa-exclamation-circle me-2"></i><?php echo $error; ?>
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<?php if ($error_imap): ?>
<div class="alert alert-warning">
<i class="fas fa-exclamation-triangle me-2"></i><?php echo $error_imap; ?>
</div>
<?php endif; ?>
<?php if (!$email_inbox_active): ?>
<div class="alert alert-info">
<i class="fas fa-info-circle me-2"></i>
Fitur inbox email belum diaktifkan. Silakan <a href="email_settings.php" class="alert-link">aktifkan pengaturan email</a> terlebih dahulu.
</div>
<?php endif; ?>
<!-- Email List -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-white">
<i class="fas fa-inbox me-2"></i>Daftar Email Masuk
</h6>
</div>
<div class="card-body">
<?php if (!empty($emails)): ?>
<div class="table-responsive">
<table class="table table-hover" id="emailTable">
<thead>
<tr>
<th>Status</th>
<th>Pengirim</th>
<th>Subjek</th>
<th>Tanggal</th>
<th>Aksi</th>
</thead>
<tbody>
<?php foreach ($emails as $index => $email): ?>
<tr class="<?php echo $email['seen'] ? '' : 'fw-bold bg-light'; ?>">
<td class="text-center">
<?php if (!$email['seen']): ?>
<span class="badge bg-primary rounded-pill">
<i class="fas fa-envelope"></i>
</span>
<?php else: ?>
<span class="badge bg-secondary rounded-pill">
<i class="fas fa-envelope-open"></i>
</span>
<?php endif; ?>
</td>
<td>
<strong><?php echo htmlspecialchars($email['from_name']); ?></strong><br>
<small class="text-muted"><?php echo htmlspecialchars($email['from']); ?></small>
</td>
<td>
<strong><?php echo htmlspecialchars($email['subject']); ?></strong><br>
<small class="text-muted"><?php echo htmlspecialchars(substr($email['body'], 0, 80)); ?>...</small>
</td>
<td><?php echo date('d/m/Y H:i', strtotime($email['date'])); ?></td>
<td>
<button class="btn btn-sm btn-primary" onclick="viewEmail(<?php echo $index; ?>)">
<i class="fas fa-eye"></i> Lihat
</button>
<button class="btn btn-sm btn-success" onclick="replyEmail(<?php echo $index; ?>)">
<i class="fas fa-reply"></i> Balas
</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php else: ?>
<div class="text-center py-5">
<i class="fas fa-inbox fa-4x text-muted mb-3"></i>
<h5>Kotak Masuk Kosong</h5>
<p class="text-muted">Belum ada email masuk atau tidak dapat terhubung ke server.</p>
</div>
<?php endif; ?>
</div>
</div>
</div>
<!-- Modal Lihat Email -->
<div class="modal fade" id="viewEmailModal" tabindex="-1">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="viewEmailSubject"></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<strong>Dari:</strong> <span id="viewEmailFrom"></span>
</div>
<div class="mb-3">
<strong>Tanggal:</strong> <span id="viewEmailDate"></span>
</div>
<hr>
<div class="mb-3">
<strong>Isi Pesan:</strong>
<div id="viewEmailBody" class="p-3 bg-light rounded" style="min-height: 200px;"></div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Tutup</button>
<button type="button" class="btn btn-success" id="replyFromViewBtn">
<i class="fas fa-reply me-2"></i>Balas
</button>
</div>
</div>
</div>
</div>
<!-- Modal Balas Email -->
<div class="modal fade" id="replyEmailModal" tabindex="-1">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<form method="POST" action="<?php echo BASE_URL; ?>/admin/email_inbox.php">
<div class="modal-header">
<h5 class="modal-title">Balas Email</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<input type="hidden" name="original_id" id="replyOriginalId">
<div class="mb-3">
<label class="form-label">Kepada</label>
<input type="email" class="form-control" name="to_email" id="replyTo" readonly>
</div>
<div class="mb-3">
<label class="form-label">Subjek</label>
<input type="text" class="form-control" name="subject" id="replySubject" readonly>
</div>
<div class="mb-3">
<label class="form-label">Pesan</label>
<textarea class="form-control" name="message" id="replyMessage" rows="8" required></textarea>
<small class="text-muted">Gunakan format teks biasa atau HTML</small>
</div>
<div class="mb-3">
<label class="form-label">Pesan Asli:</label>
<div id="originalMessagePreview" class="p-3 bg-light rounded small" style="max-height: 200px; overflow-y: auto;"></div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
<button type="submit" name="send_reply" class="btn btn-primary">
<i class="fas fa-paper-plane me-2"></i>Kirim Balasan
</button>
</div>
</form>
</div>
</div>
</div>
<script>
const BASE_URL = '<?php echo BASE_URL; ?>';
// Data email dari PHP
let emailData = <?php echo json_encode($emails); ?>;
function viewEmail(index) {
const email = emailData[index];
document.getElementById('viewEmailSubject').textContent = email.subject;
document.getElementById('viewEmailFrom').innerHTML = `${email.from_name} <${email.from}>`;
document.getElementById('viewEmailDate').textContent = email.date;
document.getElementById('viewEmailBody').innerHTML = email.body_full.replace(/\n/g, '<br>');
// Set reply button
const replyBtn = document.getElementById('replyFromViewBtn');
replyBtn.onclick = function() { replyEmail(index); };
new bootstrap.Modal(document.getElementById('viewEmailModal')).show();
}
function replyEmail(index) {
const email = emailData[index];
document.getElementById('replyOriginalId').value = email.id;
document.getElementById('replyTo').value = email.from;
document.getElementById('replySubject').value = email.subject;
document.getElementById('originalMessagePreview').innerHTML = email.body_full.replace(/\n/g, '<br>');
// Set default reply message
const replyMessage = `\n\n\n--- Pesan Asli ---\nDari: ${email.from_name}\nTanggal: ${email.date}\nSubjek: ${email.subject}\n\n${email.body_full}`;
document.getElementById('replyMessage').value = replyMessage;
new bootstrap.Modal(document.getElementById('replyEmailModal')).show();
}
function refreshInbox() {
window.location.reload();
}
</script>
<style>
#emailTable tbody tr {
cursor: pointer;
transition: all 0.2s;
}
#emailTable tbody tr:hover {
background-color: #f0f2f5;
}
#emailTable tbody tr.fw-bold {
font-weight: 600;
}
</style>
<?php require_once 'includes/footer.php'; ?>