Skip to content

Commit

Permalink
Allow to fetch emails from nested folders - closes #3623
Browse files Browse the repository at this point in the history
  • Loading branch information
freescout-help-desk committed Dec 22, 2023
1 parent 54e5aa2 commit 1618bb1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
4 changes: 3 additions & 1 deletion app/Console/Commands/FetchEmails.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ public function fetch($mailbox)

if ($folder) {
$folders[] = $folder;
} else {
$this->line('['.date('Y-m-d H:i:s').'] Could not get folder: '.$folder_name);
}
}
// try {
Expand All @@ -185,7 +187,7 @@ public function fetch($mailbox)
}

foreach ($folders as $folder) {
$this->line('['.date('Y-m-d H:i:s').'] Folder: '.$folder->name);
$this->line('['.date('Y-m-d H:i:s').'] Folder: '.($folder->full_name ?? $folder->name));

// Requesting emails by bunches allows to fetch large amounts of emails
// without problems with memory.
Expand Down
46 changes: 28 additions & 18 deletions app/Http/Controllers/MailboxesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -706,25 +706,10 @@ public function ajax(Request $request)

$imap_folders = $client->getFolders();

$response['folders'] = [];

if (count($imap_folders)) {
foreach ($imap_folders as $imap_folder) {
if (!empty($imap_folder->name)) {
$response['folders'][] = $imap_folder->name;
}
// Maybe we need a recursion here.
if (!empty($imap_folder->children)) {
foreach ($imap_folder->children as $child_imap_folder) {
// Old library.
if (!empty($child_imap_folder->fullName)) {
$response['folders'][] = $child_imap_folder->fullName;
}
// New library.
if (!empty($child_imap_folder->full_name)) {
$response['folders'][] = $child_imap_folder->full_name;
}
}
}
}
$response = $this->interateFolders($response, $imap_folders);
}

if (count($response['folders'])) {
Expand Down Expand Up @@ -813,6 +798,31 @@ public function ajax(Request $request)
return \Response::json($response);
}

// Recursively interate over folders.
public function interateFolders($response, $imap_folders) {
foreach ($imap_folders as $imap_folder) {
if (!empty($imap_folder->name)) {
$response['folders'][] = $imap_folder->name;
}

// Check for children and recurse.
if (!empty($imap_folder->children)) {
$response = $this->interateFolders($response, $imap_folder->children);
}

// Old library.
if (!empty($imap_folder->fullName)) {
$response['folders'][] = $imap_folder->fullName;
}
// New library.
if (!empty($imap_folder->full_name)) {
$response['folders'][] = $imap_folder->full_name;
}
}

return $response;
}

public function oauth(Request $request)
{
$mailbox_id = $request->id ?? '';
Expand Down

0 comments on commit 1618bb1

Please sign in to comment.