Added Fetch Email on page load

This commit is contained in:
Gitea
2025-04-26 00:22:03 +05:30
parent 9aa1b3ab8e
commit 89f6410578
16 changed files with 136 additions and 267 deletions

View File

@@ -1,62 +0,0 @@
<?php
namespace App\Livewire;
use App\Models\ZEmail;
use Livewire\Component;
use Livewire\WithPagination;
class EmailInbox extends Component
{
use WithPagination;
public $currentEmail;
public $messages = [];
public $selectedMessage = null;
public $searchTerm = '';
public $refreshInterval = 30; // seconds
public function mount()
{
$this->currentEmail = ZEmail::getEmail();
$this->loadMessages();
}
public function loadMessages()
{
if ($this->currentEmail) {
$this->messages = ZEmail::getMessages($this->currentEmail);
}
}
public function selectMessage($messageId)
{
$this->selectedMessage = $messageId;
}
public function deleteMessage($messageId)
{
ZEmail::deleteMessage($messageId);
$this->loadMessages();
$this->selectedMessage = null;
}
public function generateNewEmail()
{
$this->currentEmail = ZEmail::generateRandomEmail();
$this->loadMessages();
}
public function getPollingInterval()
{
return $this->refreshInterval * 1000; // Convert to milliseconds
}
public function render()
{
return view('livewire.email-inbox', [
'messages' => $this->messages,
'currentEmail' => $this->currentEmail
]);
}
}

View File

@@ -65,7 +65,8 @@ class Action extends Component
$this->dispatch('closeModal');
}
public function deleteEmail() {
public function deleteEmail(): void
{
ZEmail::removeEmail($this->email);
// if (count($this->emails) <= 1 && json_decode(config('app.settings.configuration_settings'))->after_last_email_delete == 'redirect_to_homepage') {
// return redirect()->route('home');
@@ -82,7 +83,8 @@ class Action extends Component
$this->dispatch('showAlert', ['type' => $type, 'message' => $message]);
}
private function checkEmailLimit() {
private function checkEmailLimit(): bool
{
$logs = Log::select('ip', 'email')->where('ip', request()->ip())->where('created_at', '>', Carbon::now()->subDay())->groupBy('email')->groupBy('ip')->get();
if (count($logs) >= json_decode(config('app.settings.configuration_settings'))->email_limit) {
return false;
@@ -90,7 +92,8 @@ class Action extends Component
return true;
}
private function checkUsedEmail() {
private function checkUsedEmail(): bool
{
if (json_decode(config('app.settings.configuration_settings'))->disable_used_email) {
$check = Log::where('email', $this->user . '@' . $this->domain)->where('ip', '<>', request()->ip())->count();
if ($check > 0) {

View File

@@ -0,0 +1,15 @@
<?php
namespace App\Livewire\Frontend\Components;
use Livewire\Component;
class Mail extends Component
{
public $message;
public function render()
{
return view('livewire.frontend.components.mail')->with(['message' => $this->message]);
}
}

View File

@@ -47,6 +47,8 @@ class Email extends Component
$this->dispatch('getEmail');
}
$this->checkMultipleEmails();
$this->dispatch('syncMailbox', $this->email);
$this->dispatch('fetchMessages');
}
public function generateEmail(): void

View File

@@ -7,30 +7,34 @@ use App\Models\ZEmail;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
class App extends Component
class Mailbox extends Component
{
public $messages = [];
public $deleted = [];
public $error = '';
public $email;
public $initial;
public $initial = false;
public $type;
public $overflow = false;
protected $listeners = ['fetchMessages' => 'fetch', 'syncEmail'];
protected $listeners = ['fetchMessages' => 'fetch', 'syncMailbox' => 'syncEmail'];
public function mount()
public function mount(): void
{
$this->email = ZEmail::getEmails();
$this->email = ZEmail::getEmail();
$this->initial = false;
}
public function syncEmail($email): void
{
$this->email = $email;
$this->initial = false;
}
public function syncEmail($email) {
$this->email = $email;
}
public function fetch() {
public function fetch(): void
{
try {
$count = count($this->messages);
$responses = [];
if (config('app.beta_feature') || !json_decode(config('app.settings.imap_settings'))->cc_check) {
$responses = [
@@ -46,18 +50,36 @@ class App extends Component
'cc' => ZEmail::getMessages($this->email, 'cc', $this->deleted)
];
}
// $responses = [
// 'to' => ZEmail::getMessages($this->email, 'to', $this->deleted),
// ];
// $imapSettings = json_decode(config('app.settings.imap_settings'));
// $betaFeature = config('app.beta_feature');
//
// if ($betaFeature || empty($imapSettings?->cc_check)) {
// $responses['cc'] = [
// 'data' => [],
// 'notifications' => []
// ];
// } else {
// $responses['cc'] = ZEmail::getMessages($this->email, 'cc', $this->deleted);
// }
$this->deleted = [];
$this->messages = array_merge($responses['to']['data'], $responses['cc']['data']);
$notifications = array_merge($responses['to']['notifications'], $responses['cc']['notifications']);
if (count($notifications)) {
if ($this->overflow == false && count($this->messages) == $count) {
if (!$this->overflow && count($this->messages) == $count) {
$this->overflow = true;
}
} else {
$this->overflow = false;
}
foreach ($notifications as $notification) {
$this->dispatchBrowserEvent('showNewMailNotification', $notification);
$this->dispatch('showNewMailNotification', $notification);
}
ZEmail::incrementMessagesStats(count($notifications));
} catch (\Exception $e) {
@@ -67,8 +89,8 @@ class App extends Component
$this->error = 'Not able to connect to Mail Server';
}
}
$this->dispatchBrowserEvent('stopLoader');
$this->dispatchBrowserEvent('loadDownload');
$this->dispatch('stopLoader');
$this->dispatch('loadDownload');
$this->initial = true;
}
@@ -88,8 +110,9 @@ class App extends Component
public function render()
{
return view('livewire.frontend.app');
return view('livewire.frontend.mailbox')->with(['messages' => $this->messages, 'email' => $this->email, 'initial' => $this->initial, 'error' => $this->error]);
}
private function rrmdir($dir): void
{
if (is_dir($dir)) {
@@ -105,5 +128,4 @@ class App extends Component
rmdir($dir);
}
}
}

View File

@@ -123,7 +123,6 @@ class Message extends Model
} else {
$search->addCondition(new To($email));
}
$search->addCondition(new Since((new \DateTime('-1 day'))));
$messages = $mailbox->getMessages($search, \SORTDATE, true);
$limit = json_decode(config('app.settings.configuration_settings'))->fetch_messages_limit ?? 15;
$count = 1;
@@ -148,11 +147,16 @@ class Message extends Model
}
$datediff = new Carbon($date);
$content = '';
$contentText = '';
$html = $message->getBodyHtml();
$text = $message->getBodyText();
if ($text) {
$contentText = str_replace('<a', '<a target="blank"', str_replace(array("\r\n", "\n"), '', $text));
}
if ($html) {
$content = str_replace('<a', '<a target="blank"', $html);
} else {
$text = $message->getBodyText();
$content = str_replace('<a', '<a target="blank"', str_replace(array("\r\n", "\n"), '<br/>', $text));
}
if (json_decode(config('app.settings.configuration_settings'))->enable_masking_external_link) {
@@ -167,6 +171,7 @@ class Message extends Model
$obj['datediff'] = $datediff->diffForHumans();
$obj['id'] = $message->getNumber();
$obj['content'] = $content;
$obj['contentText'] = $contentText;
$obj['attachments'] = [];
//Checking if Sender is Blocked
$domain = explode('@', $obj['sender_email'])[1];
@@ -224,125 +229,4 @@ class Message extends Model
$connection->expunge();
return $response;
}
// public static function fetchMessages($email, $type = 'to', $deleted = []): array
// {
// $startTime = microtime(true);
// $allowed = explode(',', 'doc,docx,xls,xlsx,ppt,pptx,xps,pdf,dxf,ai,psd,eps,ps,svg,ttf,zip,rar,tar,gzip,mp3,mpeg,wav,ogg,jpeg,jpg,png,gif,bmp,tif,webm,mpeg4,3gpp,mov,avi,mpegs,wmv,flx,txt');
// $connection = ZEmail::connectMailBox();
//
// $mailbox = $connection->getMailbox('INBOX');
// $search = new SearchExpression();
// if ($type == 'cc') {
// $search->addCondition(new Cc($email));
// } else {
// $search->addCondition(new To($email));
// }
// $search->addCondition(new Since((new \DateTime('-1 day'))));
// $stepStart = microtime(true);
// $messages = $mailbox->getMessages($search, \SORTDATE, true);
// \Log::info("1111 messages took: " . (microtime(true) - $stepStart));
//
// $limit = json_decode(config('app.settings.configuration_settings'))->fetch_messages_limit ?? 15;
// $count = 1;
// $response = [
// 'data' => [],
// 'notifications' => []
// ];
//
// foreach ($messages as $message) {
// if (in_array($message->getNumber(), $deleted)) {
// $message->delete();
// continue;
// }
// $blocked = false;
// $sender = $message->getFrom();
// $date = $message->getDate();
// if (!$date) {
// $date = new \DateTime();
// if ($message->getHeaders()->get('udate')) {
// $date->setTimestamp($message->getHeaders()->get('udate'));
// }
// }
// $datediff = new Carbon($date);
// $content = '';
// $html = $message->getBodyHtml();
// if ($html) {
// $content = str_replace('<a', '<a target="blank"', $html);
// } else {
// $text = $message->getBodyText();
// $content = str_replace('<a', '<a target="blank"', str_replace(array("\r\n", "\n"), '<br/>', $text));
// }
// if (json_decode(config('app.settings.configuration_settings'))->enable_masking_external_link) {
// $content = str_replace('href="', 'href="http://href.li/?', $content);
// }
// $obj = [];
// $obj['subject'] = $message->getSubject();
// $obj['sender_name'] = $sender->getName();
// $obj['sender_email'] = $sender->getAddress();
// $obj['timestamp'] = $message->getDate();
// $obj['date'] = $date->format(json_decode(config('app.settings.configuration_settings'))->date_format ?? 'd M Y h:i A');
// $obj['datediff'] = $datediff->diffForHumans();
// $obj['id'] = $message->getNumber();
// $obj['content'] = $content;
// $obj['attachments'] = [];
// //Checking if Sender is Blocked
// $domain = explode('@', $obj['sender_email'])[1];
// $blocked = in_array($domain, json_decode(config('app.settings.configuration_settings'))->blocked_domains);
// if ($blocked) {
// $obj['subject'] = __('Blocked');
// $obj['content'] = __('Emails from') . ' ' . $domain . ' ' . __('are blocked by Admin');
// }
// if ($message->hasAttachments() && !$blocked) {
// $attachments = $message->getAttachments();
// $directory = './tmp/attachments/' . $obj['id'] . '/';
// is_dir($directory) || mkdir($directory, 0777, true);
// foreach ($attachments as $attachment) {
// $filenameArray = explode('.', $attachment->getFilename());
// $extension = $filenameArray[count($filenameArray) - 1];
// if (in_array($extension, $allowed)) {
// if (!file_exists($directory . $attachment->getFilename())) {
// file_put_contents(
// $directory . $attachment->getFilename(),
// $attachment->getDecodedContent()
// );
// }
// if ($attachment->getFilename() !== 'undefined') {
// $url = config('app.settings.app_base_url') . str_replace('./', '/', $directory . $attachment->getFilename());
// $structure = $attachment->getStructure();
// if (isset($structure->id) && str_contains($obj['content'], trim($structure->id, '<>'))) {
// $obj['content'] = str_replace('cid:' . trim($structure->id, '<>'), $url, $obj['content']);
// }
// $obj['attachments'][] = [
// 'file' => $attachment->getFilename(),
// 'url' => $url
// ];
// }
// }
// }
// }
// $response['data'][] = $obj;
// if (!$message->isSeen()) {
// $response['notifications'][] = [
// 'subject' => $obj['subject'],
// 'sender_name' => $obj['sender_name'],
// 'sender_email' => $obj['sender_email']
// ];
// if (config('app.zemail_log')) {
// file_put_contents(storage_path('logs/zemail.csv'), request()->ip() . "," . date("Y-m-d h:i:s a") . "," . $obj['sender_email'] . "," . $email . PHP_EOL, FILE_APPEND);
// }
// }
// $message->markAsSeen();
// if (++$count > $limit) {
// break;
// }
// }
//
// $response['data'] = array_reverse($response['data']);
// $connection->expunge();
// $endTime = microtime(true);
// $executionTime = $endTime - $startTime;
// \Log::info("getMessages execution time: {$executionTime} seconds");
// return $response;
// }
}