Added almost all features except language, ads, seo, pages
This commit is contained in:
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