Skip to content

Instantly share code, notes, and snippets.

@jpalala
Created October 1, 2025 06:47
Show Gist options
  • Save jpalala/7fa32e29e66de7058e8ba060a1f02485 to your computer and use it in GitHub Desktop.
Save jpalala/7fa32e29e66de7058e8ba060a1f02485 to your computer and use it in GitHub Desktop.

Revisions

  1. jpalala created this gist Oct 1, 2025.
    13 changes: 13 additions & 0 deletions imail_inbox.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    <?php
    /* aka message list */
    require 'vendor/autoload.php';
    $redis = new Predis\Client();

    $user = $_GET['user'];

    $ids = $redis->lrange("imail:messages:$user", 0, -1);

    foreach ($ids as $id) {
    $meta = $redis->hgetall("imail:msg:$id");
    echo "[{$meta['status']}] {$meta['created_at']} - {$meta['sender']} - {$meta['subject']}<br>";
    }
    22 changes: 22 additions & 0 deletions imail_read.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    <?php
    require 'vendor/autoload.php';
    $redis = new Predis\Client();

    $user = $_POST['user'];
    $id = $_POST['id'];
    $password = $_POST['password'] ?? null;

    $meta = $redis->hgetall("imail:msg:$id");
    if (!$meta || $meta['recipient'] !== $user) {
    die("Message not found or not yours.");
    }

    $body = file_get_contents($meta['file']);
    $plain = decryptMessage($body, $meta['encryption'], $password);

    // Mark as read
    $redis->hset("imail:msg:$id", "status", "read");

    echo "<h3>From: {$meta['sender']}</h3>";
    echo "<h4>Subject: {$meta['subject']}</h4>";
    echo "<pre>" . htmlspecialchars($plain) . "</pre>";
    35 changes: 35 additions & 0 deletions imail_send.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    <?php
    require 'vendor/autoload.php'; // predis for Redis
    $redis = new Predis\Client();

    $sender = $_POST['sender'];
    $recipient = $_POST['recipient'];
    $subject = $_POST['subject'];
    $body = $_POST['body'];
    $method = $_POST['encryption'] ?? 'none';
    $password = $_POST['password'] ?? null;

    $id = uniqid($sender . '-', true);
    $file = "/home/$recipient/imail/$id.msg";

    $encryptedBody = encryptMessage($body, $method, $password);

    // Save message file
    file_put_contents($file, $encryptedBody);

    // Save metadata in Redis
    $meta = [
    'id' => $id,
    'sender' => $sender,
    'recipient' => $recipient,
    'subject' => $subject,
    'encryption' => $method,
    'status' => 'unread',
    'file' => $file,
    'created_at' => date('c')
    ];

    $redis->hmset("imail:msg:$id", $meta);
    $redis->rpush("imail:messages:$recipient", $id);

    echo "Message sent!";
    48 changes: 48 additions & 0 deletions read_send_with_decrpytion_encryption.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    use Illuminate\Support\Facades\Crypt;
    use Illuminate\Support\Facades\Redis;

    public function send(Request $request)
    {
    $body = $request->input('body');
    $to = $request->input('to');
    $from = auth()->user()->name;

    $encrypted = Crypt::encryptString($body);

    // Store in DB or file
    $id = uniqid("msg_", true);
    file_put_contents(storage_path("imail/{$to}/{$id}.msg"), $encrypted);

    // Store metadata in Redis
    Redis::hmset("imail:meta:{$id}", [
    'from' => $from,
    'to' => $to,
    'subject' => $request->input('subject', ''),
    'created_at' => now(),
    'status' => 'unread',
    ]);

    return response()->json(['status' => 'sent', 'id' => $id]);
    }
    Read Controller

    php
    Copy code
    use Illuminate\Support\Facades\Crypt;
    use Illuminate\Support\Facades\Redis;

    public function read($id)
    {
    // Load encrypted body
    $encrypted = file_get_contents(storage_path("imail/" . auth()->user()->name . "/{$id}.msg"));

    $decrypted = Crypt::decryptString($encrypted);

    // Mark as read
    Redis::hset("imail:meta:{$id}", 'status', 'read');

    return response()->json([
    'body' => $decrypted,
    'meta' => Redis::hgetall("imail:meta:{$id}")
    ]);
    }