Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions .devilbox/www/htdocs/assets/js/html-email.js

This file was deleted.

75 changes: 52 additions & 23 deletions .devilbox/www/htdocs/mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,28 @@
require $LIB_DIR . DIRECTORY_SEPARATOR . 'Mail.php';
require $LIB_DIR . DIRECTORY_SEPARATOR . 'Sort.php';

if (isset($_GET['get-body']) && is_numeric($_GET['get-body'])) {
$messageNumber = $_GET['get-body'];
$MyMbox = new \devilbox\Mail('/var/mail/devilbox');
$message = $MyMbox->getMessage($messageNumber-1);
$structure = $message['decoded'];

$body = null;
if (isset($structure->body)) {
$body = $structure->body;
}
elseif(isset($structure->parts[1]->body)) {
$body = $structure->parts[1]->body;
}
elseif(isset($structure->parts[0]->body)) {
$body = $structure->parts[0]->body;
}

exit(json_encode(array(
'raw' => htmlentities($message['raw']),
'body' => $body,
)));
}

if (isset($_GET['delete']) && is_numeric($_GET['delete'])) {
$message = $_GET['delete'];
Expand Down Expand Up @@ -171,17 +193,6 @@
<?php
$message = htmlentities($data['raw']);
$structure = $data['decoded'];
$body = null;

if (isset($structure->body)) {
$body = $structure->body;
}
elseif(isset($structure->parts[1]->body)) {
$body = $structure->parts[1]->body;
}
elseif(isset($structure->parts[0]->body)) {
$body = $structure->parts[0]->body;
}
?>
<tr id="<?php echo $data['num'];?>" class="subject">
<td><?php echo $data['num'];?></td>
Expand All @@ -198,17 +209,13 @@
<tr id="mail-<?php echo $data['num'];?>" style="display:none">
<td></td>
<td colspan="5">
<?php if ($body !== null): ?>
<template id="mail-body-<?=$data['num']?>"><?=$body?></template>
<html-email data-template-id="mail-body-<?=$data['num']?>"></html-email>
<?php else: ?>
<div class="alert alert-warning" role="alert">
No valid body found
</div>
<?php endif; ?>
<div class="email-body"></div>
<div class="alert alert-warning" role="alert" style="display:none">
No valid body found
</div>
<hr>
<p><a class="btn btn-primary" data-toggle="collapse" href="#email-<?php echo $data['num'];?>" aria-expanded="false" aria-controls="email-<?php echo $data['num'];?>">Raw source</a></p>
<div class="collapse" id="email-<?php echo $data['num'];?>"><pre><?php echo $message;?></pre></div>
<div class="collapse" id="email-<?php echo $data['num'];?>"><pre class="raw-email-body"></pre></div>
</td>
</tr>
<?php endforeach; ?>
Expand All @@ -227,12 +234,34 @@
<script>
$(function() {
$('.subject').click(function() {
var id = ($(this).attr('id'));
$('#mail-'+id).toggle();
const id = ($(this).attr('id'));
const row = $('#mail-'+id);
row.toggle();

const bodyElement = row.find('.email-body')[0];
if(bodyElement.shadowRoot !== null){
// We've already fetched the message content.
return;
}

bodyElement.attachShadow({ mode: 'open' });
bodyElement.shadowRoot.innerHTML = 'Loading...';

$.get('?get-body=' + id, function(response){
response = JSON.parse(response);
row.find('.raw-email-body').html(response.raw);

const body = response.body;
if(body === null){
row.find('.alert').show();
}
else{
bodyElement.shadowRoot.innerHTML = body;
}
})
})
// Handler for .ready() called.
});
</script>
<script src="/assets/js/html-email.js"></script>
</body>
</html>
24 changes: 17 additions & 7 deletions .devilbox/www/include/lib/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ public function delete($message) {
}


/**
* Returns a single message
*
* @param int $messageIndex The zero-based index of the message to return.
*/
public function getMessage($messageIndex){
$message = $this->_Mbox->get($messageIndex);
$Decoder = new \Mail_mimeDecode($message, "\r\n");
return array(
'num' => $messageIndex + 1,
'raw' => $message,
'decoded' => $Decoder->decode($this->_defaultMimeParams)
);
}


/**
* Retrieve emails.
*
Expand All @@ -87,13 +103,7 @@ public function get($sort = null)

// Get messages in reverse order (last entry first)
for ($n = $total; $n >= 0; --$n) {
$message = $this->_Mbox->get($n);
$Decoder = new \Mail_mimeDecode($message, "\r\n");
$messages[] = array(
'num' => $n + 1,
'raw' => $message,
'decoded' => $Decoder->decode($this->_defaultMimeParams)
);
$messages[] = $this->getMessage($n);
}

// Optionally sort messages
Expand Down
2 changes: 1 addition & 1 deletion .tests/tests/intranet-email.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ run "curl -sS --fail -XPOST 'http://localhost:${HOST_PORT_HTTPD}/mail.php' -d 'e
# Validate
run "curl -sS --fail 'http://localhost:${HOST_PORT_HTTPD}/mail.php' | tac | tac | grep '${MY_MAIL}' >/dev/null" "${RETRIES}"
run "curl -sS --fail 'http://localhost:${HOST_PORT_HTTPD}/mail.php' | tac | tac | grep '${MY_SUBJ}' >/dev/null" "${RETRIES}"
run "curl -sS --fail 'http://localhost:${HOST_PORT_HTTPD}/mail.php' | tac | tac | grep '${MY_MESS}' >/dev/null" "${RETRIES}"
run "curl -sS --fail 'http://localhost:${HOST_PORT_HTTPD}/mail.php?get-body=1' | tac | tac | grep '${MY_MESS}' >/dev/null" "${RETRIES}"