From c63e5581e5c625a6c7ef748efa10e881b592231c Mon Sep 17 00:00:00 2001 From: David Carr Date: Thu, 27 Feb 2025 08:55:15 +0000 Subject: [PATCH 1/3] added fine by name method --- src/Resources/Emails/Folders.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Resources/Emails/Folders.php b/src/Resources/Emails/Folders.php index bff64d9..454342e 100644 --- a/src/Resources/Emails/Folders.php +++ b/src/Resources/Emails/Folders.php @@ -36,6 +36,12 @@ public function find(string $id): array return MsGraph::get('me/mailFolders/'.$id); } + public function findByName(string $name): array + { + $response = MsGraph::get("me/mailFolders?\$filter=startswith(displayName,'$name')"); + return $response['value'][0] ?? []; + } + public function store(array $data): array { EmailFolderStoreValidator::validate($data); From 2ceab6aa90110da014eb68bc9d853fe67f87be86 Mon Sep 17 00:00:00 2001 From: David Carr Date: Thu, 27 Feb 2025 08:59:48 +0000 Subject: [PATCH 2/3] updated docs and updated internall emails()->get method --- docs/v3/msgraph/emails.md | 46 +++++++++++++++++++++++++-------- src/Resources/Emails/Emails.php | 42 ++++++++++++++++-------------- 2 files changed, 58 insertions(+), 30 deletions(-) diff --git a/docs/v3/msgraph/emails.md b/docs/v3/msgraph/emails.md index 7f8e95b..861770e 100644 --- a/docs/v3/msgraph/emails.md +++ b/docs/v3/msgraph/emails.md @@ -14,11 +14,13 @@ MsGraph::emails(); Return a list of emails +If no options are set emails are loaded from the inbox folder. + ```php -MsGraph::emails()->get(); +MsGraph::emails()->get($folderIdOrName = 'Inbox', $params = []); ``` -By default, only 10 emails are returned this can be changed by either using GET requests or pass an array of option to get() +By default, only 25 emails are returned this can be changed by either using GET requests or pass an array of option to get() Option 1: GET Request @@ -34,21 +36,21 @@ The default array that is used internally is below, you can override these optio ```php [ - "\$orderby" => "displayName", - "\$top" => $top, - "\$skip" => $skip, - "\$count" => "true", + '$orderby' => "displayName", + '$top' => $top, + '$skip' => $skip, + '$count' => "true", ] ``` This would look like this: ```php -MsGraph::emails()->get([ - "\$orderby" => "displayName", - "\$top" => 15, - "\$skip" => 0, - "\$count" => "true", +MsGraph::emails()->get('Inbox', [ + '$orderby' => "displayName", + '$top' => 15, + '$skip' => 0, + '$count' => "true", ]); ``` @@ -259,6 +261,28 @@ MsGraph::emails()->folders()->get(array $params = [], bool $sort = false, array MsGraph::emails()->folders()->find($id) ``` +## Get folder by name + +```php +MsGraph::emails()->folders()->findByName($name) +``` + +## Get emails from folder + +```php + +//optional parameters +$params = [ + '$skip' => $skip, + '$top' => $limit, + '$count' => 'true', +]; + +MsGraph::emails()->folders()->emails($folderId, $params); +``` + + + ## Create folder ```php diff --git a/src/Resources/Emails/Emails.php b/src/Resources/Emails/Emails.php index f76d07d..0a0178f 100644 --- a/src/Resources/Emails/Emails.php +++ b/src/Resources/Emails/Emails.php @@ -3,6 +3,7 @@ namespace Dcblogdev\MsGraph\Resources\Emails; use Dcblogdev\MsGraph\Facades\MsGraph; +use Dcblogdev\MsGraph\Validators\GraphQueryValidator; use Exception; class Emails extends MsGraph @@ -114,42 +115,39 @@ public function skip(string $skip): static /** * @throws Exception */ - public function get(string $folderId = '', array $params = []): array + public function get(string $folderIdOrName = 'Inbox', array $params = []): array { + GraphQueryValidator::validate($params); + $top = request('top', $this->top); $skip = request('skip', $this->skip); - if ($top === null) { - $top = 100; + if ($top === '') { + $top = 25; } - if ($skip === null) { + if ($skip === '') { $skip = 0; } if ($params === []) { - $params = http_build_query([ + $params = [ '$top' => $top, '$skip' => $skip, '$count' => 'true', - ]); - } else { - $params = http_build_query($params); + ]; } - $folder = $folderId == '' ? 'Inbox' : $folderId; - - // get inbox from folders list - $folder = MsGraph::get("me/mailFolders?\$filter=startswith(displayName,'$folder')"); - - if (isset($folder['value'][0])) { - // folder id - $folderId = $folder['value'][0]['id']; + if ($this->isId($folderIdOrName)) { + $folder = MsGraph::emails()->folders()->find($folderIdOrName); + } else { + $folder = MsGraph::emails()->folders()->findByName($folderIdOrName); + } - // get messages from folderId - return MsGraph::get("me/mailFolders/$folderId/messages?".$params); + if ($folder !== []) { + return MsGraph::get("me/mailFolders/".$folder['id']."/messages?".http_build_query($params)); } else { - throw new Exception('email folder not found'); + throw new Exception('Email folder not found'); } } @@ -339,4 +337,10 @@ protected function prepareEmail(): array return $envelope; } + + private function isId(string $value): bool + { + // IDs are long, contain uppercase/lowercase letters, numbers, hyphens, dots, underscores, and end with '=' + return preg_match('/^[A-Za-z0-9\-_]+={0,2}$/', $value) && strlen($value) > 50; + } } From 16a789e9e3ac888ef99c5a3fdb8b24960e902a22 Mon Sep 17 00:00:00 2001 From: David Carr Date: Thu, 27 Feb 2025 09:10:16 +0000 Subject: [PATCH 3/3] updated docs --- docs/v3/msgraph/emails.md | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/docs/v3/msgraph/emails.md b/docs/v3/msgraph/emails.md index 861770e..f7b0cd0 100644 --- a/docs/v3/msgraph/emails.md +++ b/docs/v3/msgraph/emails.md @@ -267,22 +267,6 @@ MsGraph::emails()->folders()->find($id) MsGraph::emails()->folders()->findByName($name) ``` -## Get emails from folder - -```php - -//optional parameters -$params = [ - '$skip' => $skip, - '$top' => $limit, - '$count' => 'true', -]; - -MsGraph::emails()->folders()->emails($folderId, $params); -``` - - - ## Create folder ```php