HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/iphim.naniguide.com/common/Logging/Mail/OutgoingEmailLogController.php
<?php

namespace Common\Logging\Mail;

use Common\Core\BaseController;
use Common\Database\Datasource\Datasource;
use ZBateson\MailMimeParser\Message;

class OutgoingEmailLogController extends BaseController
{
    public function __construct()
    {
        $this->middleware('isAdmin');
    }

    public function show(int $id): mixed
    {
        $logItem = OutgoingEmailLogItem::findOrFail($id);

        $message = Message::from($logItem->mime, true);

        $logItem->parsed_message = [
            'headers' => collect($message->getAllHeaders())->mapWithKeys(
                fn($header) => [$header->getName() => $header->getValue()],
            ),
            'body' => [
                'text' => $message->getTextContent(),
                'html' => $message->getHtmlContent(),
            ],
        ];

        return $this->success([
            'logItem' => $logItem,
        ]);
    }

    public function index(): mixed
    {
        $pagination = (new Datasource(
            OutgoingEmailLogItem::query(),
            request()->all(),
        ))->paginate();

        return $this->success([
            'pagination' => $pagination,
        ]);
    }

    public function downloadLog()
    {
        $log = json_encode(
            OutgoingEmailLogItem::limit(1000)->get(),
            JSON_PRETTY_PRINT,
        );

        return response($log)
            ->header('Content-Type', 'application/json')
            ->header(
                'Content-Disposition',
                'attachment; filename="outgoing-email-log.json"',
            );
    }

    public function downloadLogItem(int $id)
    {
        $logItem = OutgoingEmailLogItem::findOrFail($id);

        return response($logItem->mime)
            ->header('Content-Type', 'message/rfc822')
            ->header(
                'Content-Disposition',
                "attachment; filename=\"{$logItem->subject}.eml\"",
            );
    }
}