<!
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Read Email</title>
<script src="https://cdn.jsdelivr.net/npm/@yaireo/tagify"></script>
<link href="https://cdn.jsdelivr.net/npm/@yaireo/tagify/dist/tagify.css"
rel="stylesheet" type="text/css" />
<style>
.image-container {
margin-bottom: 20px;
}
.image-container img {
max-width: 100%;
height: auto;
}
</style>
</head>
<body>
<h1>Read Email</h1>
<?php
if (isset($_GET['alert']) && $_GET['alert'] === '1') {
echo '<script>alert("Pesan tidak boleh kosong!");</script>';
}
if (isset($_GET['alert']) && $_GET['alert'] === '2') {
echo '<script>alert("Pesan Berhasil dikirim");</script>';
}
// Konfigurasi IMAP
$server = '{Mail.youdomain.com:143/notls}'; // Ganti dengan server IMAP Anda
$username = '[email protected]';
$password = 'password';
$mailbox = 'INBOX';
// Membuka koneksi IMAP
$connection = imap_open($server . $mailbox, $username, $password);
if (!$connection) {
die('Tidak dapat terhubung ke server IMAP: ' . imap_last_error());
}
// Mendapatkan ID email dari URL
$mailId = $_GET['mailId'];
// Mendapatkan informasi tentang pesan email
$header = imap_headerinfo($connection, $mailId);
$subject = mb_decode_mimeheader($header->subject);
$from = $header->from;
$fromName = isset($from[0]->personal) ? $from[0]->personal : ''; // Mendapatkan
nama pemilik email
$fromEmail = $from[0]->mailbox . "@" . $from[0]->host; // Mendapatkan alamat
email pengirim
$timestamp = strtotime($header->date);
$timeDiff = time() - $timestamp;
if ($timeDiff < 86400) { // Jika pesan belum lewat 24 jam
$date = date("i", $timeDiff) . " menit yang lalu";
} else {
$date = date("d M Y H:i:s", $timestamp); // Format tanggal yang diubah
}
// Mendapatkan struktur pesan email
$structure = imap_fetchstructure($connection, $mailId);
// Fungsi untuk mendapatkan lampiran
function getAttachments($structure, $connection, $mailId) {
$attachments = array();
if (isset($structure->parts) && count($structure->parts)) {
foreach ($structure->parts as $key => $part) {
// Memeriksa apakah bagian memiliki lampiran
if (isset($part->disposition) && strtolower($part->disposition) ==
"attachment") {
$filename = isset($part->dparameters[0]->value) ? $part-
>dparameters[0]->value : ''; // Nama file lampiran
$attachment = imap_fetchbody($connection, $mailId, $key +
1); // Mengambil lampiran
$attachments[] = array(
'filename' => $filename,
'attachment' => $attachment
);
}
}
}
return $attachments;
}
// Fungsi untuk mendapatkan isi pesan email
function getMessage($connection, $mailId) {
// Mengambil isi pesan email
$message = imap_fetchbody($connection, $mailId, 1.1);
// Jika pesan dalam format HTML, gunakan alternatif untuk mengambil pesan
if (empty($message)) {
$message = imap_fetchbody($connection, $mailId, 1);
}
// Hapus tag HTML dan tampilkan dalam tag <pre> untuk mempertahankan format
tabel
$message = strip_tags($message, '<table><tr><td>'); // Membiarkan hanya tag
tabel yang diizinkan
$message = "<pre>$message</pre>";
return $message;
}
// Fungsi untuk mendapatkan gambar dari konten HTML pesan email
function getImagesFromHTML($htmlContent) {
$images = array();
$dom = new DOMDocument();
@$dom->loadHTML($htmlContent);
$imgTags = $dom->getElementsByTagName('img');
foreach ($imgTags as $imgTag) {
$src = $imgTag->getAttribute('src');
if ($src) {
$images[] = $src;
}
}
return $images;
}
// Mendapatkan gambar dari konten HTML pesan email
$message = getMessage($connection, $mailId);
$images = getImagesFromHTML($message);
// Mendapatkan foto profil Gravatar
$gravatarUrl = 'https://www.gravatar.com/avatar/' .
md5(strtolower(trim($fromEmail))) . '?d=identicon&s=200';
// Menampilkan foto profil Gravatar
echo "<img src='$gravatarUrl' alt='Profile Picture'><br>";
// Menampilkan gambar
if (!empty($images)) {
echo "<h3>Gambar:</h3>";
foreach ($images as $image) {
echo "<div class='image-container'><a href='$image'
target='_blank'><img src='$image' alt='Embedded Image'></a></div>";
}
}
// Mendapatkan lampiran jika ada
$attachments = getAttachments($structure, $connection, $mailId);
// Menampilkan informasi pesan email secara terperinci
echo "<h2>Subject: $subject</h2>";
echo "<p>From: $fromName <$fromEmail></p>"; // Tampilkan nama pemilik
email dan alamat email pengirim
echo "<p>Date: $date</p>";
echo "<p>Message: $message</p>";
// Menampilkan Message-ID dari email
$messageId = imap_uid($connection, $mailId);
echo "<p>Message-ID: $messageId</p>";
// Menampilkan lampiran jika ada
if (!empty($attachments)) {
echo "<h3>Lampiran:</h3>";
foreach ($attachments as $attachment) {
$filename = $attachment['filename'];
$attachmentData = $attachment['attachment'];
$fileExtension = pathinfo($filename, PATHINFO_EXTENSION);
// Menyimpan lampiran ke direktori yang bisa diakses oleh web
server
$filePath = 'attachments/' . $filename;
file_put_contents($filePath, $attachmentData);
// Menampilkan gambar langsung dengan tag img
if (in_array($fileExtension, ['jpg', 'jpeg', 'png', 'gif'])) {
echo "<div class='image-container'><img src='$filePath'
alt='$filePath'></div>";
}
// Menampilkan tautan unduh
echo "<p><a href='download.php?file=$filename'>$filename</a></p>";
echo "<a href='download.php?file=$filename'>Download</a>";
}
}
if (isset($_GET['mailId'])) {
$mailId = $_GET['mailId'];
} else {
// Tampilkan pesan kesalahan jika parameter tidak ditemukan
echo "Error: Mail ID not found in URL.";
exit; // Keluar dari skrip karena parameter tidak ada
}
// Menutup koneksi IMAP
imap_close($connection);
?>
<h2>Reply to Email</h2>
<form action="reply.php" method="post">
<input type="hidden" name="mailId" value="<?php echo $mailId; ?>">
<input type="hidden" name="replyMessageId" value="<?php echo $messageId; ?
>">
<input type="hidden" name="name" value="<?php echo $fromName; ?>">
<input type="hidden" name="email" value="<?php echo $fromEmail; ?>">
<input type="hidden" name="subject" value="<?php echo $subject; ?>">
<textarea name="replyMessage" rows="4" cols="50" placeholder="Type your
reply here..."></textarea><br>
<button type="submit" name="reply">Reply</button>
</form>
<!-- Form untuk forward -->
<h2>Forward Email</h2>
<form action="forward.php" method="post">
<input type="hidden" name="mailId" value="<?php echo $mailId; ?>">
<input type="hidden" name="name" value="<?php echo $fromName; ?>">
<input type="hidden" name="email" value="<?php echo $fromEmail; ?>">
<input type="hidden" name="subject" value="<?php echo $subject; ?>">
<input type="text" name="recipient[]" id="recipient" value="tag1, tag2"
placeholder="Recipient's Email"><br>
<textarea name="forwardMessage" rows="4" cols="50" placeholder="Type your
message here..."></textarea><br>
<button type="submit" name="forward">Forward</button>
</form>
<script>
// The DOM element you wish to replace with Tagify
var input = document.querySelector('input[name=recipient[]]');
// initialize Tagify on the above input node reference
new Tagify(input)
</script>
</body>
</html>