185 lines
6.2 KiB
PHP
185 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Ui\Webmail;
|
|
|
|
use App\Models\MailUser;
|
|
use App\Services\ImapService;
|
|
use Livewire\Attributes\Layout;
|
|
use Livewire\Attributes\Title;
|
|
use Livewire\Attributes\Url;
|
|
use Livewire\Component;
|
|
|
|
#[Layout('layouts.webmail')]
|
|
#[Title('Nachricht · Webmail')]
|
|
class MailView extends Component
|
|
{
|
|
#[Url(as: 'folder')]
|
|
public string $folder = 'INBOX';
|
|
|
|
public int $uid = 0;
|
|
public array $message = [];
|
|
|
|
public function mount(int $uid): void
|
|
{
|
|
if (! session('webmail_email')) {
|
|
$this->redirect(route('ui.webmail.login'));
|
|
return;
|
|
}
|
|
$this->uid = $uid;
|
|
$this->loadMessage();
|
|
}
|
|
|
|
private function loadMessage(): void
|
|
{
|
|
try {
|
|
$imap = app(ImapService::class);
|
|
$client = $imap->client(
|
|
session('webmail_email'),
|
|
session('webmail_password'),
|
|
);
|
|
|
|
$prefs = MailUser::where('email', session('webmail_email'))->value('webmail_prefs') ?? [];
|
|
$autoMarkRead = (bool) ($prefs['auto_mark_read'] ?? true);
|
|
|
|
if ($this->folder !== 'Drafts' && $autoMarkRead) {
|
|
$imap->markSeen($client, $this->folder, $this->uid);
|
|
}
|
|
$this->message = $imap->getMessage($client, $this->folder, $this->uid);
|
|
$client->disconnect();
|
|
} catch (\Throwable $e) {
|
|
\Illuminate\Support\Facades\Log::warning('MailView load failed', ['error' => $e->getMessage()]);
|
|
session()->forget(['webmail_email', 'webmail_password']);
|
|
$this->redirect(route('ui.webmail.login'));
|
|
}
|
|
}
|
|
|
|
public function delete(): void
|
|
{
|
|
try {
|
|
$imap = app(ImapService::class);
|
|
$client = $imap->client(
|
|
session('webmail_email'),
|
|
session('webmail_password'),
|
|
);
|
|
|
|
if ($this->folder === 'Trash') {
|
|
// Already in trash → permanent delete
|
|
$imap->deleteMessage($client, $this->folder, $this->uid);
|
|
$this->dispatch('toast', type: 'done', badge: 'Webmail',
|
|
title: 'Gelöscht', text: 'Nachricht wurde endgültig gelöscht.', duration: 3000);
|
|
} else {
|
|
// Move to trash (soft delete)
|
|
$imap->moveMessage($client, $this->folder, $this->uid, 'Trash');
|
|
$this->dispatch('toast', type: 'done', badge: 'Webmail',
|
|
title: 'In Papierkorb', text: 'Nachricht wurde in den Papierkorb verschoben.', duration: 3000);
|
|
}
|
|
$client->disconnect();
|
|
} catch (\Throwable) {}
|
|
|
|
$this->redirect(route('ui.webmail.inbox', ['folder' => $this->folder]));
|
|
}
|
|
|
|
public function markSpam(): void
|
|
{
|
|
try {
|
|
$imap = app(ImapService::class);
|
|
$client = $imap->client(
|
|
session('webmail_email'),
|
|
session('webmail_password'),
|
|
);
|
|
$imap->moveMessage($client, $this->folder, $this->uid, 'Junk');
|
|
$client->disconnect();
|
|
$this->dispatch('toast', type: 'done', badge: 'Webmail',
|
|
title: 'Als Spam markiert', text: 'Nachricht in Spam-Ordner verschoben.', duration: 3000);
|
|
} catch (\Throwable) {}
|
|
|
|
$this->redirect(route('ui.webmail.inbox', ['folder' => $this->folder]));
|
|
}
|
|
|
|
public function notSpam(): void
|
|
{
|
|
try {
|
|
$imap = app(ImapService::class);
|
|
$client = $imap->client(
|
|
session('webmail_email'),
|
|
session('webmail_password'),
|
|
);
|
|
$imap->moveMessage($client, $this->folder, $this->uid, 'INBOX');
|
|
$client->disconnect();
|
|
$this->dispatch('toast', type: 'done', badge: 'Webmail',
|
|
title: 'Kein Spam', text: 'Nachricht in den Posteingang verschoben.', duration: 3000);
|
|
} catch (\Throwable) {}
|
|
|
|
$this->redirect(route('ui.webmail.inbox', ['folder' => $this->folder]));
|
|
}
|
|
|
|
public function archive(): void
|
|
{
|
|
try {
|
|
$imap = app(ImapService::class);
|
|
$client = $imap->client(
|
|
session('webmail_email'),
|
|
session('webmail_password'),
|
|
);
|
|
$imap->moveMessage($client, $this->folder, $this->uid, 'Archive');
|
|
$client->disconnect();
|
|
$this->dispatch('toast', type: 'done', badge: 'Webmail',
|
|
title: 'Archiviert', text: 'Nachricht wurde archiviert.', duration: 3000);
|
|
} catch (\Throwable) {}
|
|
|
|
$this->redirect(route('ui.webmail.inbox', ['folder' => $this->folder]));
|
|
}
|
|
|
|
public function toggleFlag(): void
|
|
{
|
|
try {
|
|
$imap = app(ImapService::class);
|
|
$client = $imap->client(
|
|
session('webmail_email'),
|
|
session('webmail_password'),
|
|
);
|
|
$flagged = $imap->toggleFlag($client, $this->folder, $this->uid);
|
|
$client->disconnect();
|
|
$this->message['flagged'] = $flagged;
|
|
} catch (\Throwable) {}
|
|
}
|
|
|
|
public function markUnseen(): void
|
|
{
|
|
try {
|
|
$imap = app(ImapService::class);
|
|
$client = $imap->client(
|
|
session('webmail_email'),
|
|
session('webmail_password'),
|
|
);
|
|
$imap->markUnseen($client, $this->folder, $this->uid);
|
|
$client->disconnect();
|
|
} catch (\Throwable) {}
|
|
|
|
$this->redirect(route('ui.webmail.inbox', ['folder' => $this->folder]));
|
|
}
|
|
|
|
public function moveTo(string $target): void
|
|
{
|
|
$allowed = ['INBOX', 'Sent', 'Drafts', 'Archive', 'Junk', 'Trash'];
|
|
if (! in_array($target, $allowed, true) || $target === $this->folder) return;
|
|
|
|
try {
|
|
$imap = app(ImapService::class);
|
|
$client = $imap->client(
|
|
session('webmail_email'),
|
|
session('webmail_password'),
|
|
);
|
|
$imap->moveMessage($client, $this->folder, $this->uid, $target);
|
|
$client->disconnect();
|
|
} catch (\Throwable) {}
|
|
|
|
$this->redirect(route('ui.webmail.inbox', ['folder' => $this->folder]));
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.ui.webmail.mail-view');
|
|
}
|
|
}
|