Skip to content

Commit

Permalink
fix: fixed imap for all folders now just little handling left
Browse files Browse the repository at this point in the history
  • Loading branch information
devansh-webkul committed Sep 20, 2024
1 parent d0569c1 commit 3abb19e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public function update($id)
*/
public function inboundParse(InboundEmailProcessor $inboundEmailProcessor)
{
$inboundEmailProcessor->process(request('email'));
$inboundEmailProcessor->processMessage(request('email'));

return response()->json([], 200);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ public function handle()
{
$this->info('Processing the incoming emails.');

$messages = $this->inboundEmailProcessor->getMessages();

foreach ($messages as $message) {
$this->inboundEmailProcessor->process($message);
}
$this->inboundEmailProcessor->processMessagesFromAllFolders();

$this->info('Incoming emails processed successfully.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ interface InboundEmailProcessor
*
* @return mixed
*/
public function getMessages();
public function processMessagesFromAllFolders();

/**
* Process the inbound email.
*
* @param mixed|null $content
*/
public function process($content = null): void;
public function processMessage($content = null): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,20 @@ public function __construct(
/**
* Get the messages from the mail server.
*/
public function getMessages()
public function processMessagesFromAllFolders()
{
/**
* SendGrid's Inbound Parse is a specialized tool for developers to handle incoming emails in
* their applications, but it doesn't replace the full functionality of IMAP for typical
* email client usage. Thats why we can't get the messages from the mail server.
*/
throw new \Exception('Currently bulk processing is not supported for Sendgrid.');
}

/**
* Process the inbound email.
*/
public function process($message = null): void
public function processMessage($message = null): void
{
$this->emailParser->setText($message);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
class WebklexImapEmailProcessor implements InboundEmailProcessor
{
/**
* Folder name.
* The IMAP client instance.
*/
protected const FOLDER_NAME = 'INBOX';
protected $client;

/**
* Create a new repository instance.
Expand All @@ -22,29 +22,33 @@ class WebklexImapEmailProcessor implements InboundEmailProcessor
public function __construct(
protected EmailRepository $emailRepository,
protected AttachmentRepository $attachmentRepository
) {}
) {
$this->client = Client::account('default');

$this->client->connect();

if (! $this->client->isConnected()) {
throw new \Exception('Failed to connect to the mail server.');
}
}

/**
* Close the connection.
*/
public function __destruct()
{
$this->client->disconnect();
}

/**
* Get the messages from the mail server.
*/
public function getMessages()
public function processMessagesFromAllFolders()
{
try {
$client = Client::account('default');

$client->connect();

if (! $client->isConnected()) {
throw new \Exception('Failed to connect to the mail server.');
}
$rootFolders = $this->client->getFolders();

$folder = $client->getFolder(self::FOLDER_NAME);

$messages = $folder->query()->since(now()->subDays(10))->get();

$client->disconnect();

return $messages;
$this->processMessagesFromLeafFolders($rootFolders);
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
Expand All @@ -53,7 +57,7 @@ public function getMessages()
/**
* Process the inbound email.
*/
public function process($message = null): void
public function processMessage($message = null): void
{
$attributes = $message->getAttributes();

Expand Down Expand Up @@ -84,7 +88,7 @@ public function process($message = null): void
$email = $this->emailRepository->create([
'from' => $attributes['from']->first()->mail,
'subject' => $attributes['subject']->first(),
'name' => $attributes['subject']->first(),
'name' => $attributes['from']->first()->personal,
'reply' => $message->bodies['html'] ?? $message->bodies['text'],
'is_read' => 0,
'folders' => [strtolower('inbox')],
Expand All @@ -107,4 +111,24 @@ public function process($message = null): void
]);
}
}

/**
* Process the messages from all folders.
*
* @param \Webklex\IMAP\Support\FolderCollection $rootFoldersCollection
*/
protected function processMessagesFromLeafFolders($rootFoldersCollection = null): void
{
$rootFoldersCollection->each(function ($folder) {
if (! $folder->children->isEmpty()) {
$this->processMessagesFromLeafFolders($folder->children);

return;
}

return $folder->query()->since(now()->subDays(10))->get()->each(function ($message) {
$this->processMessage($message);
});
});
}
}

0 comments on commit 3abb19e

Please sign in to comment.