75
common/Logging/Mail/OutgoingEmailLogController.php
Executable file
75
common/Logging/Mail/OutgoingEmailLogController.php
Executable file
@@ -0,0 +1,75 @@
|
||||
<?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\"",
|
||||
);
|
||||
}
|
||||
}
|
||||
55
common/Logging/Mail/OutgoingEmailLogItem.php
Executable file
55
common/Logging/Mail/OutgoingEmailLogItem.php
Executable file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Logging\Mail;
|
||||
|
||||
use Common\Core\BaseModel;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
|
||||
class OutgoingEmailLogItem extends BaseModel
|
||||
{
|
||||
const MODEL_TYPE = 'outgoing_email_log_item';
|
||||
|
||||
protected $table = 'outgoing_email_log';
|
||||
|
||||
protected $guarded = ['id'];
|
||||
|
||||
protected $casts = [
|
||||
'id' => 'integer',
|
||||
];
|
||||
|
||||
protected $hidden = ['mime'];
|
||||
|
||||
protected function mime(): Attribute
|
||||
{
|
||||
return Attribute::make(get: fn(string $value) => utf8_decode($value));
|
||||
}
|
||||
|
||||
public static function filterableFields(): array
|
||||
{
|
||||
return ['id', 'status', 'from', 'to', 'created_at'];
|
||||
}
|
||||
|
||||
public function toNormalizedArray(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->subject,
|
||||
'description' => $this->message_id,
|
||||
'model_type' => self::MODEL_TYPE,
|
||||
];
|
||||
}
|
||||
|
||||
public function toSearchableArray(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'subject' => $this->subject,
|
||||
'to' => $this->to,
|
||||
];
|
||||
}
|
||||
|
||||
public static function getModelTypeAttribute(): string
|
||||
{
|
||||
return self::MODEL_TYPE;
|
||||
}
|
||||
}
|
||||
51
common/Logging/Mail/OutgoingEmailLogSubscriber.php
Executable file
51
common/Logging/Mail/OutgoingEmailLogSubscriber.php
Executable file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Logging\Mail;
|
||||
|
||||
use Illuminate\Events\Dispatcher;
|
||||
use Illuminate\Mail\Events\MessageSending;
|
||||
use Illuminate\Mail\Events\MessageSent;
|
||||
|
||||
class OutgoingEmailLogSubscriber
|
||||
{
|
||||
public function handleSending(MessageSending $event): void
|
||||
{
|
||||
$headers = $event->message->getPreparedHeaders();
|
||||
|
||||
$logItem = OutgoingEmailLogItem::create([
|
||||
'message_id' => $headers->get('Message-ID')->getBodyAsString(),
|
||||
'from' => $headers->get('From')->getBodyAsString(),
|
||||
'to' => $headers->get('To')->getBodyAsString(),
|
||||
'subject' => $headers->get('Subject')->getBodyAsString(),
|
||||
'mime' => utf8_encode($event->message->toString()),
|
||||
'status' => 'not-sent',
|
||||
]);
|
||||
|
||||
$event->message
|
||||
->getHeaders()
|
||||
->addTextHeader('X-BE-LOG-ID', $logItem->id);
|
||||
}
|
||||
|
||||
public function handleSent(MessageSent $event): void
|
||||
{
|
||||
$logId = $event->message
|
||||
->getHeaders()
|
||||
->get('X-BE-LOG-ID')
|
||||
->getBodyAsString();
|
||||
|
||||
OutgoingEmailLogItem::where('id', $logId)->update([
|
||||
'status' => 'sent',
|
||||
'message_id' => $event->sent
|
||||
->getSymfonySentMessage()
|
||||
->getMessageId(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function subscribe(Dispatcher $events): array
|
||||
{
|
||||
return [
|
||||
MessageSending::class => 'handleSending',
|
||||
MessageSent::class => 'handleSent',
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user