[go: up one dir, main page]

0% found this document useful (0 votes)
40 views8 pages

Script Ok

The document contains code for an email inbox application built with PHP and IMAP. It includes functions for marking emails as read/unread, moving emails between folders, and searching emails. It also implements pagination for listing emails.

Uploaded by

Adellin Yolanda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views8 pages

Script Ok

The document contains code for an email inbox application built with PHP and IMAP. It includes functions for marking emails as read/unread, moving emails between folders, and searching emails. It also implements pagination for listing emails.

Uploaded by

Adellin Yolanda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

<script>

function processMove(selectedValue) {
var moveFolderInput = document.getElementsByName('MoveFolder')[0];
moveFolderInput.value = selectedValue;
document.getElementById('myForm').submit(); // Submit form

}
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<style>
tr.unread {
background-color: #f2f2f2; /* Ganti dengan warna yang Anda inginkan */
}

/* Ganti gaya teks pesan yang belum dibaca */


tr.unread td {
font-weight: bold;
color: #000;
}
.replied {
font-weight: bold;
color: #255;
}
.forwarded {
font-weight: bold;
color: #400;
}

.pagination {
margin-top: 20px;
}
.pagination a {
padding: 5px 10px;
border: 1px solid #ccc;
margin-right: 5px;
text-decoration: none;
}
.pagination a.active {
background-color: #007bff;
color: #fff;
}
</style>
</head>
<body>
<?php
// Konfigurasi IMAP
$server = '{mail.centraltranscargo.com:143/notls}'; // Ganti dengan server IMAP
Anda
$username = 'contact@centraltranscargo.com';
$password = 'cinta17@';

// Membuka koneksi IMAP


$connection = imap_open($server, $username, $password);

if (!$connection) {
die('Tidak dapat terhubung ke server IMAP: ' . imap_last_error());
}
if (isset($_POST['refresh'])) {
refreshInbox();
}

// Mark email as read


function markAsRead($connection, $mailId) {
imap_setflag_full($connection, $mailId, "\\Seen");
}

if (isset($_POST['markRead']) && isset($_POST['mailIds'])) {


$mailIds = $_POST['mailIds'];
if (!empty($mailIds)) {
foreach ($mailIds as $mailId) {
markAsRead($connection, $mailId);
}
// Refresh page
refreshInbox();
} else {
echo "Tidak ada email yang dipilih untuk di Read.<br>";
}
}

// Mark email as unread


function markAsUnread($connection, $mailId) {
imap_clearflag_full($connection, $mailId, "\\Seen");
}

if (isset($_POST['markUnread']) && isset($_POST['mailIds'])) {


$mailIds = $_POST['mailIds'];
if (!empty($mailIds)) {
foreach ($mailIds as $mailId) {
markAsUnread($connection, $mailId);
}
// Refresh page
refreshInbox();
} else {
echo "Tidak ada email yang dipilih untuk di Unread.<br>";
}
}

// Copy email to a specified folder


function copyEmail($connection, $mailId, $sourceFolder, $targetFolder) {
// Copy email to the target folder
$result = imap_mail_copy($connection, $mailId, $targetFolder);
if ($result) {
echo "Email copied successfully.<br>";
} else {
echo "Failed to copy email: " . imap_last_error() . "<br>";
}
}

// Move email to a specified folder


function moveEmail($connection, $mailId, $sourceFolder, $targetFolder) {
// Move email from the source folder to the target folder
$result = imap_mail_move($connection, $mailId, $targetFolder);
if ($result) {
// Delete the email from the source folder after moving it
imap_delete($connection, $mailId);
// Execute the delete command permanently
imap_expunge($connection);
echo "Email moved successfully.<br>";
} else {
echo "Failed to move email: " . imap_last_error() . "<br>";
}
}

// Check if MoveFolder is set and mailIds is set


if (isset($_POST['MoveFolder']) && isset($_POST['mailIds'])) {
$mailIds = $_POST['mailIds'];
$MoveFolder = $_POST['MoveFolder'];
$sourceFolder = '{mail.centraltranscargo.com:143/notls}INBOX'; // Adjust this
according to your source folder
if (!empty($mailIds)) {
foreach ($mailIds as $mailId) {
moveEmail($connection, $mailId, $sourceFolder, $MoveFolder);
}
// Refresh page
refreshInbox();
} else {
echo "No emails selected to move.<br>";
}
}

if (isset($_POST['MoveToTrash']) && isset($_POST['mailIds'])) {


$mailIds = $_POST['mailIds'];
$sourceFolder = '{mail.centraltranscargo.com:143/notls}INBOX'; //
Tambahkan ini
if (!empty($mailIds)) {
foreach ($mailIds as $mailId) {
moveEmail($connection, $mailId, $sourceFolder,
'INBOX.Trash'); // Perbaiki pemanggilan fungsi moveEmail()
}
// Refresh page
refreshInbox();
} else {
echo "Tidak ada email yang dipilih untuk dihapus.<br>";
}
}

// Mendapatkan pesan email


$searchKeyword = isset($_POST['searchKeyword']) ?
$_POST['searchKeyword'] : '';
$searchCriteria = $searchKeyword ? "TEXT \"$searchKeyword\"" : 'ALL';
$mails = imap_search($connection, $searchCriteria);

if ($mails !== false) {


// Memutar urutan array agar pesan terbaru muncul di atas
$mails = array_reverse($mails);
// Menghitung jumlah email
$totalEmails = count($mails);
// Menghitung jumlah email yang belum dibaca
$unreadEmails = 0;
foreach ($mails as $mailId) {
$header = imap_headerinfo($connection, $mailId);
if ($header->Unseen == 'U') {
$unreadEmails++;
}
}
// Menghitung jumlah email baru (dalam 7 Hari terakhir)
$newEmails = 0;
foreach ($mails as $mailId) {
$header = imap_headerinfo($connection, $mailId);
$timestamp = strtotime($header->date);
$timeDiff = time() - $timestamp;
if ($timeDiff < 604800 && $header->Unseen == 'U') {
$newEmails++;
}
}

echo "<section class='content-header'>


<h1>
Mailbox
<small>$unreadEmails new messages</small>
</h1>
<ol class='breadcrumb'>
<li><a href='#'><i class='fa fa-dashboard'></i> Home</a></li>
<li class='active'>Mailbox</li>
</ol>
</section>
<!-- Main content -->
<section class='content'>
<div class='row'>
<div class='col-md-3'>
<a href='compose.html' class='btn btn-primary btn-block margin-
bottom'>Compose</a>
<div class='box box-solid'>
<div class='box-header with-border'>
<h3 class='box-title'>Folders</h3>
<div class='box-tools'>
<button class='btn btn-box-tool' data-widget='collapse'><i
class='fa fa-minus'></i></button>
</div>
</div>
<div class='box-body no-padding nav-tabs-custom'>
<ul class='nav nav-pills nav-stacked'>
<li class='active'><a href='?module=mailcontact&fol=inbox' data-
toggle='tab'><i class='fa fa-inbox'></i> Inbox <span class='label label-primary
pull-right'>12</span></a></li>
<li><a href='?module=mailcontact&fol=sent'><i class='fa fa-
envelope-o'></i> Sent</a></li>
<li><a href='#'><i class='fa fa-file-text-o'></i> Drafts</a></li>
<li><a href='#'><i class='fa fa-filter'></i> Junk <span
class='label label-warning pull-right'>65</span></a></li>
<li><a href='#'><i class='fa fa-trash-o'></i> Trash</a></li>
</ul>
</div><!-- /.box-body -->
</div><!-- /. box -->
<div class='box box-solid'>
<div class='box-header with-border'>
<h3 class='box-title'>Labels</h3>
<div class='box-tools'>
<button class='btn btn-box-tool' data-widget='collapse'><i
class='fa fa-minus'></i></button>
</div>
</div>
<div class='box-body no-padding'>
<ul class='nav nav-pills nav-stacked'>
<li><a href='#'><i class='fa fa-circle-o text-red'></i>
Important</a></li>
<li><a href='#'><i class='fa fa-circle-o text-yellow'></i>
Promotions</a></li>
<li><a href='#'><i class='fa fa-circle-o text-light-blue'></i>
Social</a></li>
</ul>
</div><!-- /.box-body -->
</div><!-- /.box -->
</div><!-- /.col -->
<div class='col-md-9'>
<div class='box box-danger'>
<div class='box-header with-border'>
<h3 class='box-title'>Inbox</h3>
<div class='box-tools pull-right'>
<div class='has-feedback'>
<input type='text' class='form-control input-sm'
placeholder='Search Mail'>
<span class='glyphicon glyphicon-search
form-control-feedback'></span>
</div>
</div><!-- /.box-tools -->
</div><!-- /.box-header -->";

// Pagination
$emailsPerPage = 10; // Jumlah email per halaman
$totalPages = ceil($totalEmails / $emailsPerPage); // Jumlah total halaman
$currentPage = isset($_GET['page']) ? $_GET['page'] : 1; // Halaman saat
ini
$startIndex = ($currentPage - 1) * $emailsPerPage; // Indeks awal email
untuk halaman ini
$endIndex = min($startIndex + $emailsPerPage, $totalEmails); // Indeks
akhir email untuk halaman ini

// Menampilkan tabel dengan daftar email


echo "
<div class='box-body no-padding'>
<div class='mailbox-controls'>
<form method='post' id='myForm' action=''>
<!-- Check all button -->
<a class='btn btn-default btn-sm checkbox-toggle'><i class='fa fa-square-
o'></i></a>
<button class='btn btn-default btn-sm'><i class='fa
fa-trash-o'></i></button>
<button class='btn btn-default btn-sm'><i class='fa
fa-refresh'></i></button>

<div class='btn-group'>
<button type='submit' class='btn btn-default dropdown-toggle dropdown-icon
btn-sm' data-toggle='dropdown'>More
<span class='caret'></span>
<span class='sr-only'>Toggle Dropdown</span>
</button>
<ul class='dropdown-menu' role='menu'>
<li><a class='dropdown-item' href='#'><i class='fa fa-pencil'></i> Mark as
Read</a></li>
<li><a class='dropdown-item' href='#'><i class='fa fa-ban'></i>
Spam</a></li>
<li class='divider'></li>
<li><a class='dropdown-item alert_notif'><i class='fa fa-trash-o'></i>
Delete</a></li>
</ul>
</div>
<div class='btn-group'>
<button type='button' class='btn btn-default dropdown-toggle dropdown-icon
btn-sm' data-toggle='dropdown'>Move to
<span class='caret'></span>
<span class='sr-only'>Toggle Dropdown</span>
</button>
<ul class='dropdown-menu' role='menu'>
<li><a class='dropdown-item' href='#'><i class='fa fa-pencil'></i> Mark as
Read</a></li>
<li><a class='dropdown-item' href='#'><i class='fa fa-ban'></i>
Spam</a></li>
<li class='divider'></li>
<li><a class='dropdown-item alert_notif' href='#'><i class='fa fa-trash-
o'></i> Delete</a></li>
</ul>
</div>
<div class='pull-right'>";
for ($i = 1; $i <= $totalPages; $i++) {
$active = ($i == $currentPage) ? 'active' : '';
echo "<a href='?page=$i' class='$active'>$i</a>";
}
echo "<div class='btn-group'>
<button class='btn btn-default btn-sm'><i class='fa
fa-chevron-left'></i></button>
<button class='btn btn-default btn-sm'><i class='fa
fa-chevron-right'></i></button>
</div><!-- /.btn-group -->
</div><!-- /.pull-right -->
</div>
<div class='table-responsive mailbox-messages'>

<table id='mytable' width='100%' class='table table-inbox table-hover


table-striped'>";

// Menampilkan email per halaman


for ($i = $startIndex; $i < $endIndex; $i++) {
$mailId = $mails[$i];
$header = imap_headerinfo($connection, $mailId);
$structure = imap_fetchstructure($connection, $mailId);
$subject = isset($header->subject) ? $header->subject : '[No Subject]';
// Decode subject if it's encoded
$subject = mb_decode_mimeheader($subject);
// Mengecek tipe dari fromaddress
if (is_array($header->from)) {
// Jika merupakan array, kita ambil name dan address
if (isset($header->from[0]->personal)) {
$fromName = $header->from[0]->personal;
} else {
$fromName = '';
}
$fromAddress = $header->from[0]->mailbox . "@" . $header->from[0]-
>host;
} else {
$fromName = '';
$fromAddress = $header->from;
}
$timestamp = strtotime($header->date);
$timeDiff = time() - $timestamp;
if ($timeDiff < 3600) { // Jika pesan belum lewat 24 jam
$date = date("i", $timeDiff) . " menit yang lalu";
} elseif ($timeDiff > 3600 && $timeDiff > 86000) {
$date = date("H", $timeDiff) . " jam yang lalu";
} else {
$date = date("d M Y H:i:s", $timestamp); // Format tanggal yang diubah
}

// Mengecek apakah pesan sudah dibaca atau belum


$readStatus = ($header->Unseen == 'U') ? 'unread' : 'read';

// Mengecek apakah pesan sudah di-reply atau belum


$repliedStatus = (isset($header->in_reply_to)) ? '?' : '';

// Mengecek apakah pesan telah diteruskan


$forwardedStatus = strpos(strtolower($subject), 'forwarded') !== false ?
'Kanan' : '';

// Mengecek apakah pesan memiliki lampiran


$attachmentIcon = (isset($structure->parts) && count($structure->parts) >
0) ? '??' : '';

echo "<tr class='$readStatus'>


<td><input type='hidden' name='mailId' value='$mailId'><input type='checkbox'
name='mailIds[]' value='$mailId'></td>
<td>$repliedStatus $forwardedStatus</td>
<td><a href='read_email.php?mailId=$mailId'>$subject</a></td>
<td>$fromName</td>
<td>$fromAddress</td>
<td>$date</td>
<td>$attachmentIcon</td>
</tr>
</tbody>
";
}
echo "</table>";
echo "</form>";

} else {
echo " <table id='mytable' width='100%' class='table table-inbox table-hover
table-striped'>
<tr>
<td>Tidak ada email dalam kotak masuk.</td>

</tr>
</table>
";
}
echo "</div><!-- /.mail-box-messages -->
</div><!-- /.box-body -->

</div><!-- /. box -->


</div><!-- /.col -->
</div><!-- /.row -->
</section><!-- /.content -->";

// Menutup koneksi IMAP


imap_close($connection);
?>

</body>
</html>

You might also like