Added almost all features except language, ads, seo, pages
This commit is contained in:
47
app/ColorPicker.php
Normal file
47
app/ColorPicker.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
trait ColorPicker
|
||||
{
|
||||
public static function chooseColor($letter): array
|
||||
{
|
||||
$colorReferences = [
|
||||
"A" => ["dark" => "dark:bg-amber-500", "light" => "bg-amber-800"],
|
||||
"B" => ["dark" => "dark:bg-blue-500", "light" => "bg-blue-800"],
|
||||
"C" => ["dark" => "dark:bg-cyan-500", "light" => "bg-cyan-800"],
|
||||
"D" => ["dark" => "dark:bg-emerald-500", "light" => "bg-emerald-800"],
|
||||
"E" => ["dark" => "dark:bg-fuchsia-500", "light" => "bg-fuchsia-800"],
|
||||
"F" => ["dark" => "dark:bg-gray-500", "light" => "bg-gray-800"],
|
||||
"G" => ["dark" => "dark:bg-green-500", "light" => "bg-green-800"],
|
||||
"H" => ["dark" => "dark:bg-indigo-500", "light" => "bg-indigo-800"],
|
||||
"I" => ["dark" => "dark:bg-lime-500", "light" => "bg-lime-800"],
|
||||
"J" => ["dark" => "dark:bg-neutral-500", "light" => "bg-neutral-800"],
|
||||
"K" => ["dark" => "dark:bg-orange-500", "light" => "bg-orange-800"],
|
||||
"L" => ["dark" => "dark:bg-pink-500", "light" => "bg-pink-800"],
|
||||
"M" => ["dark" => "dark:bg-purple-500", "light" => "bg-purple-800"],
|
||||
"N" => ["dark" => "dark:bg-red-500", "light" => "bg-red-800"],
|
||||
"O" => ["dark" => "dark:bg-rose-500", "light" => "bg-rose-800"],
|
||||
"P" => ["dark" => "dark:bg-sky-500", "light" => "bg-sky-800"],
|
||||
"Q" => ["dark" => "dark:bg-slate-500", "light" => "bg-slate-800"],
|
||||
"R" => ["dark" => "dark:bg-stone-500", "light" => "bg-stone-800"],
|
||||
"S" => ["dark" => "dark:bg-teal-500", "light" => "bg-teal-800"],
|
||||
"T" => ["dark" => "dark:bg-violet-500", "light" => "bg-violet-800"],
|
||||
"U" => ["dark" => "dark:bg-yellow-500", "light" => "bg-yellow-800"],
|
||||
"V" => ["dark" => "dark:bg-zinc-500", "light" => "bg-zinc-800"],
|
||||
"W" => ["dark" => "dark:bg-neutral-500", "light" => "bg-neutral-800"],
|
||||
"X" => ["dark" => "dark:bg-slate-500", "light" => "bg-slate-800"],
|
||||
"Y" => ["dark" => "dark:bg-stone-500", "light" => "bg-stone-800"],
|
||||
"Z" => ["dark" => "dark:bg-teal-500", "light" => "bg-teal-800"]
|
||||
];
|
||||
|
||||
$letter = strtoupper($letter);
|
||||
|
||||
if (isset($colorReferences[$letter])) {
|
||||
return $colorReferences[$letter];
|
||||
}
|
||||
|
||||
return ["dark" => "dark:bg-gray-500", "light" => "bg-gray-800"];
|
||||
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,32 @@ class AppController extends Controller
|
||||
public function app() {
|
||||
return redirect()->route('home');
|
||||
}
|
||||
public function switch($email) {
|
||||
ZEmail::setEmail($email);
|
||||
if (json_decode(config('app.settings.configuration_settings'))->disable_mailbox_slug) {
|
||||
return redirect()->route('home');
|
||||
}
|
||||
return redirect()->route('mailbox');
|
||||
}
|
||||
|
||||
public function delete($email = null) {
|
||||
if ($email) {
|
||||
$emails = ZEmail::getEmails();
|
||||
ZEmail::removeEmail($email);
|
||||
return redirect()->route('mailbox');
|
||||
} else {
|
||||
return redirect()->route('home');
|
||||
}
|
||||
}
|
||||
|
||||
public function locale($locale) {
|
||||
if (in_array($locale, config('app.locales'))) {
|
||||
session(['locale' => $locale]);
|
||||
return redirect()->back();
|
||||
}
|
||||
abort(400);
|
||||
}
|
||||
|
||||
|
||||
private function getStringBetween($string, $start, $end) {
|
||||
$string = ' ' . $string;
|
||||
|
||||
@@ -41,8 +41,7 @@ class Action extends Component
|
||||
}
|
||||
|
||||
$this->email = ZEmail::createCustomEmail($this->username, $this->domain);
|
||||
$this->dispatch('updateEmail');
|
||||
$this->dispatch('closeModal');
|
||||
return redirect()->route('mailbox');
|
||||
|
||||
}
|
||||
public function random() {
|
||||
@@ -50,10 +49,8 @@ class Action extends Component
|
||||
return $this->showAlert('error', __('You have reached daily limit of maximum ') . json_decode(config('app.settings.configuration_settings'))->email_limit . __(' temp mail addresses.'));
|
||||
}
|
||||
$this->email = ZEmail::generateRandomEmail();
|
||||
$this->dispatch('updateEmail');
|
||||
$this->dispatch('closeModal');
|
||||
|
||||
//$this->redirect(route('mailbox'));
|
||||
return redirect()->route('mailbox');
|
||||
}
|
||||
|
||||
public function gmail() {
|
||||
@@ -61,21 +58,12 @@ class Action extends Component
|
||||
return $this->showAlert('error', __('You have reached daily limit of maximum ') . json_decode(config('app.settings.configuration_settings'))->email_limit . __(' temp mail addresses.'));
|
||||
}
|
||||
$this->email = ZEmail::generateRandomGmail();
|
||||
$this->dispatch('updateEmail');
|
||||
$this->dispatch('closeModal');
|
||||
return redirect()->route('mailbox');
|
||||
}
|
||||
|
||||
public function deleteEmail(): void
|
||||
public function deleteEmail()
|
||||
{
|
||||
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');
|
||||
// }
|
||||
$this->email = ZEmail::getEmail(true);
|
||||
$this->emails = ZEmail::getEmails();
|
||||
|
||||
$this->dispatch('updateEmail');
|
||||
$this->dispatch('closeModal');
|
||||
return redirect()->route('delete', ['email' => $this->email]);
|
||||
}
|
||||
|
||||
private function showAlert($type, $message): void
|
||||
|
||||
@@ -7,6 +7,12 @@ use Livewire\Component;
|
||||
class Mail extends Component
|
||||
{
|
||||
public $message;
|
||||
public $messageId;
|
||||
|
||||
public function setMessageId($messageId): void
|
||||
{
|
||||
$this->dispatch('setMessageId', ['messageId' => $messageId]);
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
|
||||
16
app/Livewire/Frontend/Components/Message.php
Normal file
16
app/Livewire/Frontend/Components/Message.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Frontend\Components;
|
||||
|
||||
use Livewire\Component;
|
||||
|
||||
class Message extends Component
|
||||
{
|
||||
public $message;
|
||||
public $messageId;
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.frontend.components.message');
|
||||
}
|
||||
}
|
||||
@@ -32,11 +32,9 @@ class Email extends Component
|
||||
}
|
||||
}
|
||||
|
||||
public function switchEmail($email): void
|
||||
public function switchEmail($email)
|
||||
{
|
||||
ZEmail::setEmail($email);
|
||||
$this->email = $email;
|
||||
$this->dispatch('updateEmail');
|
||||
return redirect()->route('switch', ['email' => $email]);
|
||||
}
|
||||
|
||||
public function syncEmail(): void
|
||||
|
||||
@@ -2,13 +2,17 @@
|
||||
|
||||
namespace App\Livewire\Frontend;
|
||||
|
||||
use App\ColorPicker;
|
||||
use App\Models\Message;
|
||||
use App\Models\ZEmail;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Livewire\Component;
|
||||
|
||||
class Mailbox extends Component
|
||||
{
|
||||
use ColorPicker;
|
||||
|
||||
public $messages = [];
|
||||
public $deleted = [];
|
||||
public $error = '';
|
||||
@@ -16,25 +20,32 @@ class Mailbox extends Component
|
||||
public $initial = false;
|
||||
public $type;
|
||||
public $overflow = false;
|
||||
public $messageId;
|
||||
|
||||
protected $listeners = ['fetchMessages' => 'fetch', 'syncMailbox' => 'syncEmail'];
|
||||
protected $listeners = ['fetchMessages' => 'fetch', 'syncMailbox' => 'syncEmail', 'setMessageId' => 'setMessageId'];
|
||||
|
||||
public function mount(): void
|
||||
public function mount()
|
||||
{
|
||||
$this->email = ZEmail::getEmail();
|
||||
$this->initial = false;
|
||||
if (!ZEmail::getEmail()) {
|
||||
return redirect()->route('home');
|
||||
}
|
||||
}
|
||||
public function syncEmail($email): void
|
||||
{
|
||||
$this->email = $email;
|
||||
$this->initial = false;
|
||||
$this->messages = [];
|
||||
}
|
||||
|
||||
public function fetch(): void
|
||||
{
|
||||
try {
|
||||
$count = count($this->messages);
|
||||
|
||||
if ($count > 0) {
|
||||
$this->messages = [];
|
||||
}
|
||||
$responses = [];
|
||||
if (config('app.beta_feature') || !json_decode(config('app.settings.imap_settings'))->cc_check) {
|
||||
$responses = [
|
||||
@@ -51,21 +62,6 @@ class Mailbox extends Component
|
||||
];
|
||||
}
|
||||
|
||||
// $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']);
|
||||
@@ -90,22 +86,33 @@ class Mailbox extends Component
|
||||
}
|
||||
}
|
||||
$this->dispatch('stopLoader');
|
||||
$this->dispatch('loadDownload');
|
||||
$this->initial = true;
|
||||
}
|
||||
|
||||
public function delete($messageId) {
|
||||
if (config('app.beta_feature')) {
|
||||
Message::find($messageId)->delete();
|
||||
}
|
||||
$this->deleted[] = $messageId;
|
||||
foreach ($this->messages as $key => $message) {
|
||||
if ($message['id'] == $messageId) {
|
||||
$directory = './tmp/attachments/' . $messageId;
|
||||
$this->rrmdir($directory);
|
||||
unset($this->messages[$key]);
|
||||
|
||||
try {
|
||||
if (config('app.beta_feature')) {
|
||||
Message::find($messageId)->delete();
|
||||
}
|
||||
if (config('app.fetch_from_db')) {
|
||||
\App\Models\Email::where(['message_id' => $messageId])->delete();
|
||||
}
|
||||
$this->deleted[] = $messageId;
|
||||
foreach ($this->messages as $key => $message) {
|
||||
if ($message['id'] == $messageId) {
|
||||
$directory = public_path('tmp/attachments/') . $messageId;
|
||||
$this->rrmdir($directory);
|
||||
unset($this->messages[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (
|
||||
\Exception $exception
|
||||
) {
|
||||
Log::error($exception->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function render()
|
||||
|
||||
@@ -2,10 +2,21 @@
|
||||
|
||||
namespace App\Livewire;
|
||||
|
||||
use App\Models\ZEmail;
|
||||
use Livewire\Component;
|
||||
|
||||
class Home extends Component
|
||||
{
|
||||
protected $listeners = ['fetchMessages' => 'checkIfAnyMessage'];
|
||||
|
||||
public function checkIfAnyMessage()
|
||||
{
|
||||
$email = ZEmail::getEmail();
|
||||
$messages = ZEmail::getMessages($email);
|
||||
if (count($messages['data']) > 0) {
|
||||
return redirect()->route('mailbox');
|
||||
}
|
||||
}
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.home');
|
||||
|
||||
431
app/Models/Email.php
Normal file
431
app/Models/Email.php
Normal file
@@ -0,0 +1,431 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\ColorPicker;
|
||||
use Carbon\Carbon;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Ddeboer\Imap\Search\Date\Before;
|
||||
use Ddeboer\Imap\Search\Date\Since;
|
||||
use Ddeboer\Imap\SearchExpression;
|
||||
use Ddeboer\Imap\Server;
|
||||
use Ddeboer\Imap\Search\Email\Cc;
|
||||
use Ddeboer\Imap\Search\Email\To;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class Email extends Model
|
||||
{
|
||||
|
||||
use ColorPicker;
|
||||
protected $table = 'emails';
|
||||
|
||||
// Fillable fields to allow mass assignment
|
||||
protected $fillable = [
|
||||
'message_id',
|
||||
'subject',
|
||||
'from_name',
|
||||
'from_email',
|
||||
'to',
|
||||
'cc',
|
||||
'bcc',
|
||||
'timestamp',
|
||||
'body_text',
|
||||
'body_html',
|
||||
'is_seen',
|
||||
'is_flagged',
|
||||
'size',
|
||||
'mailbox',
|
||||
'raw_headers',
|
||||
'raw_body',
|
||||
'attachments',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'to' => 'array',
|
||||
'cc' => 'array',
|
||||
'bcc' => 'array',
|
||||
'attachments' => 'array', // If attachments are stored as a JSON field
|
||||
'timestamp' => 'datetime', // Cast timestamp to Carbon instance
|
||||
];
|
||||
|
||||
public static function connectMailBox($imap = null): \Ddeboer\Imap\ConnectionInterface
|
||||
{
|
||||
if ($imap === null) {
|
||||
$imap = json_decode(config('app.settings.imap_settings'), true);
|
||||
}
|
||||
$flags = $imap['protocol'] . '/' . $imap['encryption'];
|
||||
if ($imap['validate_cert']) {
|
||||
$flags = $flags . '/validate-cert';
|
||||
} else {
|
||||
$flags = $flags . '/novalidate-cert';
|
||||
}
|
||||
$server = new Server($imap['host'], $imap['port'], $flags);
|
||||
return $server->authenticate($imap['username'], $imap['password']);
|
||||
}
|
||||
|
||||
public static function fetchProcessStoreEmail()
|
||||
{
|
||||
|
||||
try {
|
||||
$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 = \App\Models\Email::connectMailBox();
|
||||
$mailbox = $connection->getMailbox('INBOX');
|
||||
// $search = new SearchExpression();
|
||||
// $email = "gegsaf@e-pool.co.uk";
|
||||
// $search->addCondition(new To($email));
|
||||
|
||||
$messages = $mailbox->getMessages();
|
||||
//$messages = $mailbox->getMessages($search, \SORTDATE, true);
|
||||
|
||||
$result = '';
|
||||
|
||||
foreach ($messages as $message) {
|
||||
|
||||
$sender = $message->getFrom();
|
||||
$date = $message->getDate();
|
||||
if (!$date) {
|
||||
$date = new \DateTime();
|
||||
if ($message->getHeaders()->get('udate')) {
|
||||
$date->setTimestamp($message->getHeaders()->get('udate'));
|
||||
}
|
||||
}
|
||||
$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 {
|
||||
$content = str_replace('<a', '<a target="blank"', str_replace(array("\r\n", "\n"), '<br/>', $text));
|
||||
}
|
||||
|
||||
$obj = [];
|
||||
|
||||
$to = $message->getHeaders()->get('To') ? array_map(function ($entry) {
|
||||
return $entry->mailbox . '@' . $entry->host;
|
||||
}, $message->getHeaders()->get('To')) : [];
|
||||
|
||||
$cc = $message->getHeaders()->get('Cc') ? array_map(function ($entry) {
|
||||
return $entry->mailbox . '@' . $entry->host;
|
||||
}, $message->getHeaders()->get('Cc')) : [];
|
||||
|
||||
$bcc = $message->getHeaders()->get('Bcc') ? array_map(function ($entry) {
|
||||
return $entry->mailbox . '@' . $entry->host;
|
||||
}, $message->getHeaders()->get('Bcc')) : [];
|
||||
|
||||
$messageTime = $message->getDate();
|
||||
$utcTime = CarbonImmutable::instance($messageTime)->setTimezone('UTC')->toDateTimeString();
|
||||
|
||||
$obj['id'] = $message->getNumber();
|
||||
$obj['to'] = $to;
|
||||
$obj['cc'] = $cc;
|
||||
$obj['bcc'] = $bcc;
|
||||
$obj['subject'] = $message->getSubject();
|
||||
$obj['sender_name'] = $sender->getName();
|
||||
$obj['sender_email'] = $sender->getAddress();
|
||||
$obj['timestamp'] = $utcTime;
|
||||
$obj['size'] = $message->getSize();
|
||||
//$obj['date'] = $date->format(json_decode(config('app.settings.configuration_settings'))->date_format ?? 'd M Y h:i A');
|
||||
$obj['content'] = $content;
|
||||
$obj['contentText'] = $contentText;
|
||||
$obj['attachments'] = [];
|
||||
//$obj['raw_headers'] = $message->getRawHeaders();
|
||||
//$obj['raw_body'] = $message->getRawMessage();
|
||||
|
||||
if ($message->hasAttachments()) {
|
||||
$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())) {
|
||||
try {
|
||||
file_put_contents(
|
||||
$directory . $attachment->getFilename(),
|
||||
$attachment->getDecodedContent()
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
\Illuminate\Support\Facades\Log::error($e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
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()) {
|
||||
$initialData = $obj;
|
||||
$data = [
|
||||
'message_id' => Carbon::parse($utcTime)->format('Ymd').$initialData['id'],
|
||||
'subject' => $initialData['subject'],
|
||||
'from_name' => $initialData['sender_name'],
|
||||
'from_email' => $initialData['sender_email'],
|
||||
'to' => $initialData['to'],
|
||||
'cc' => $initialData['cc'],
|
||||
'bcc' => $initialData['bcc'],
|
||||
'timestamp' => $initialData['timestamp'], // store in UTC
|
||||
'body_text' => $initialData['contentText'],
|
||||
'body_html' => $initialData['content'],
|
||||
'is_seen' => false,
|
||||
'is_flagged' => false,
|
||||
'size' => $initialData['size'],
|
||||
'mailbox' => 'INBOX',
|
||||
'raw_headers' => null,
|
||||
'raw_body' => null,
|
||||
'attachments' => $initialData['attachments'],
|
||||
];
|
||||
|
||||
try {
|
||||
self::create($data);
|
||||
$checkAction = config('app.move_or_delete');
|
||||
if ($checkAction != null) {
|
||||
if ($checkAction == 'delete') {
|
||||
$message->delete();
|
||||
} else {
|
||||
$newMailBox = $connection->getMailbox($checkAction);
|
||||
$message->move($newMailBox);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (\Exception $e) {
|
||||
// \Log::error($e);
|
||||
}
|
||||
} else {
|
||||
$initialData = $obj;
|
||||
$data = [
|
||||
'message_id' => Carbon::parse($utcTime)->format('Ymd').$initialData['id'],
|
||||
'subject' => $initialData['subject'],
|
||||
'from_name' => $initialData['sender_name'],
|
||||
'from_email' => $initialData['sender_email'],
|
||||
'to' => $initialData['to'],
|
||||
'cc' => $initialData['cc'],
|
||||
'bcc' => $initialData['bcc'],
|
||||
'timestamp' => $initialData['timestamp'], // store in UTC
|
||||
'body_text' => $initialData['contentText'],
|
||||
'body_html' => $initialData['content'],
|
||||
'is_seen' => true,
|
||||
'is_flagged' => false,
|
||||
'size' => $initialData['size'],
|
||||
'mailbox' => 'INBOX',
|
||||
'raw_headers' => null,
|
||||
'raw_body' => null,
|
||||
'attachments' => $initialData['attachments'],
|
||||
];
|
||||
|
||||
try {
|
||||
self::create($data);
|
||||
$checkAction = config('app.move_or_delete');
|
||||
if ($checkAction != null) {
|
||||
if ($checkAction == 'delete') {
|
||||
$message->delete();
|
||||
} else {
|
||||
$newMailBox = $connection->getMailbox($checkAction);
|
||||
$message->move($newMailBox);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (\Exception $e) {
|
||||
// \Log::error($e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
$connection->expunge();
|
||||
|
||||
} catch (\Exception $e) {
|
||||
\Illuminate\Support\Facades\Log::error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static function fetchEmailFromDB($email) {
|
||||
|
||||
$validator = Validator::make(['email' => $email], [
|
||||
'email' => 'required|email'
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return [];
|
||||
}
|
||||
return self::whereJsonContains('to', $email)->orderBy('timestamp', 'desc')->get();
|
||||
}
|
||||
|
||||
public static function parseEmail($email, $deleted = []): array
|
||||
{
|
||||
$messages = self::fetchEmailFromDB($email);
|
||||
$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['message_id'], $deleted)) {
|
||||
// If it exists, delete the matching record from the 'emails' table
|
||||
Email::where('message_id', $message['message_id'])->delete();
|
||||
continue;
|
||||
}
|
||||
|
||||
$blocked = false;
|
||||
|
||||
$timestamp = $message['timestamp'];
|
||||
$carbonTimestamp = Carbon::parse($timestamp, 'UTC');
|
||||
$obj = [];
|
||||
$obj['subject'] = $message['subject'];
|
||||
$obj['sender_name'] = $message['from_name'];
|
||||
$obj['sender_email'] = $message['from_email'];
|
||||
$obj['timestamp'] = $message['timestamp'];
|
||||
$obj['date'] = $carbonTimestamp->format('d M Y h:i A');
|
||||
$obj['datediff'] = $carbonTimestamp->diffForHumans(Carbon::now('UTC'));
|
||||
$obj['id'] = $message['message_id'];
|
||||
$obj['content'] = $message['body_html'];
|
||||
$obj['contentText'] = $message['body_text'];
|
||||
$obj['attachments'] = [];
|
||||
$obj['is_seen'] = $message['is_seen'];
|
||||
$obj['sender_photo'] = self::chooseColor(strtoupper(substr($message['from_name'] ?: $message['from_email'], 0, 1) ));
|
||||
|
||||
|
||||
$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');
|
||||
$obj['contentText'] = __('Emails from') . ' ' . $domain . ' ' . __('are blocked by Admin');
|
||||
}
|
||||
|
||||
if (count($message['attachments']) > 0 && !$blocked) {
|
||||
$obj['attachments'] = $message['attachments'];
|
||||
|
||||
}
|
||||
|
||||
$response['data'][] = $obj;
|
||||
if (!$message['is_seen']) {
|
||||
$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);
|
||||
}
|
||||
}
|
||||
Email::where('message_id', $message['message_id'])->update(['is_seen' => true]);
|
||||
if (++$count > $limit) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
public static function deleteBulkAttachments()
|
||||
{
|
||||
$dir = public_path('/tmp/attachments');
|
||||
|
||||
try {
|
||||
if (File::exists($dir)) {
|
||||
File::cleanDirectory($dir);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
\Illuminate\Support\Facades\Log::error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static function deleteBulkMailboxes()
|
||||
{
|
||||
|
||||
|
||||
$foldersToClean = ['Trash', 'ZDUMP', 'INBOX'];
|
||||
$cutoff = (new \DateTime())->modify('-3 hours');
|
||||
$totalDeleted = 0;
|
||||
$maxToDelete = 100;
|
||||
|
||||
foreach ($foldersToClean as $folderName) {
|
||||
$connection = \App\Models\Email::connectMailBox();
|
||||
if ($totalDeleted >= $maxToDelete) {
|
||||
$connection->expunge();
|
||||
break;
|
||||
}
|
||||
|
||||
if ($connection->hasMailbox($folderName)) {
|
||||
$mailbox = $connection->getMailbox($folderName);
|
||||
$messages = $mailbox->getMessages(new Since(new \DateTime('today')));
|
||||
|
||||
foreach ($messages as $message) {
|
||||
if ($totalDeleted >= $maxToDelete) {
|
||||
$connection->expunge();
|
||||
break 2; // exit both loops
|
||||
}
|
||||
|
||||
$messageDate = $message->getDate();
|
||||
if ($messageDate < $cutoff) {
|
||||
$message->delete();
|
||||
$totalDeleted++;
|
||||
}
|
||||
}
|
||||
}
|
||||
$connection->expunge();
|
||||
}
|
||||
|
||||
return "$totalDeleted message(s) deleted from Trash and ZDUMP.";
|
||||
}
|
||||
|
||||
public static function deleteMessagesFromDB() {
|
||||
$cutoff = Carbon::now('UTC')->subHours(6)->toDateTimeString();
|
||||
$count = count(self::where('timestamp', '<', $cutoff)
|
||||
->orderBy('timestamp', 'desc')
|
||||
->get());
|
||||
|
||||
if ($count > 0) {
|
||||
self::where('timestamp', '<', $cutoff)->delete();
|
||||
return "$count old message(s) deleted from the database.";
|
||||
}
|
||||
return "No messages older than 6 hours found.";
|
||||
}
|
||||
|
||||
public static function mailToDBStatus(): bool
|
||||
{
|
||||
$latestRecord = self::orderBy('timestamp', 'desc')->first();
|
||||
|
||||
if (!$latestRecord) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$currentTime = Carbon::now('UTC');
|
||||
$lastRecordTime = Carbon::parse($latestRecord->timestamp);
|
||||
|
||||
// Check if the last record was added within the last 1 hour
|
||||
if ($lastRecordTime->diffInMinutes($currentTime) < 5) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\ColorPicker;
|
||||
use Carbon\Carbon;
|
||||
use Ddeboer\Imap\Search\Date\Since;
|
||||
use Ddeboer\Imap\Search\Email\Cc;
|
||||
@@ -14,7 +15,7 @@ use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class Message extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use HasFactory, ColorPicker;
|
||||
|
||||
public static function store(Request $request): void
|
||||
{
|
||||
@@ -149,7 +150,6 @@ class Message extends Model
|
||||
$content = '';
|
||||
$contentText = '';
|
||||
$html = $message->getBodyHtml();
|
||||
|
||||
$text = $message->getBodyText();
|
||||
if ($text) {
|
||||
$contentText = str_replace('<a', '<a target="blank"', str_replace(array("\r\n", "\n"), '', $text));
|
||||
@@ -173,12 +173,16 @@ class Message extends Model
|
||||
$obj['content'] = $content;
|
||||
$obj['contentText'] = $contentText;
|
||||
$obj['attachments'] = [];
|
||||
$obj['is_seen'] = true;
|
||||
$obj['sender_photo'] = self::chooseColor(strtoupper(substr($sender->getName() ?: $sender->getAddress(), 0, 1) ));
|
||||
|
||||
//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');
|
||||
$obj['contentText'] = __('Emails from') . ' ' . $domain . ' ' . __('are blocked by Admin');
|
||||
}
|
||||
if ($message->hasAttachments() && !$blocked) {
|
||||
$attachments = $message->getAttachments();
|
||||
@@ -189,10 +193,14 @@ class Message extends Model
|
||||
$extension = $filenameArray[count($filenameArray) - 1];
|
||||
if (in_array($extension, $allowed)) {
|
||||
if (!file_exists($directory . $attachment->getFilename())) {
|
||||
file_put_contents(
|
||||
$directory . $attachment->getFilename(),
|
||||
$attachment->getDecodedContent()
|
||||
);
|
||||
try {
|
||||
file_put_contents(
|
||||
$directory . $attachment->getFilename(),
|
||||
$attachment->getDecodedContent()
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
\Illuminate\Support\Facades\Log::error($e->getMessage());
|
||||
}
|
||||
}
|
||||
if ($attachment->getFilename() !== 'undefined') {
|
||||
$url = config('app.settings.app_base_url') . str_replace('./', '/', $directory . $attachment->getFilename());
|
||||
|
||||
@@ -14,11 +14,6 @@ use function str_replace;
|
||||
|
||||
class ZEmail extends Model
|
||||
{
|
||||
public static function check()
|
||||
{
|
||||
return ZEmail::createCustomEmail(username: 'sdcs', domain: 'e-pool.uk');
|
||||
}
|
||||
|
||||
public static function connectMailBox($imap = null): \Ddeboer\Imap\ConnectionInterface
|
||||
{
|
||||
if ($imap === null) {
|
||||
@@ -39,6 +34,16 @@ class ZEmail extends Model
|
||||
if (config('app.beta_feature')) {
|
||||
return Message::getMessages($email);
|
||||
}
|
||||
if (config('app.force_db_mail')) {
|
||||
return Email::parseEmail($email, $deleted);
|
||||
}
|
||||
if (config('app.fetch_from_db')) {
|
||||
if (Email::mailToDBStatus()) {
|
||||
return Email::parseEmail($email, $deleted);
|
||||
} else {
|
||||
return Message::fetchMessages($email, $type, $deleted);
|
||||
}
|
||||
}
|
||||
return Message::fetchMessages($email, $type, $deleted);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user