Skip to content

Commit 84230aa

Browse files
committed
Working on docs
1 parent 8c91946 commit 84230aa

File tree

1 file changed

+142
-12
lines changed

1 file changed

+142
-12
lines changed

readme.md

Lines changed: 142 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,31 +92,161 @@ $inbox = $mailbox->folders()->inbox();
9292
$folders = $mailbox->folders()->get();
9393

9494
// Get all mailbox's folders matching a glob pattern.
95-
$folders = $mailbox->folders('*/Subfolder')->get();
95+
$folders = $mailbox->folders()->get('*/Subfolder');
9696

9797
// Find a specific folder.
9898
$folder = $mailbox->folders()->find('Folder Name');
9999
```
100100

101101
#### Retrieving Messages
102102

103+
ImapEngine provides a fluent, chainable API for building advanced message search queries.
104+
105+
This allows you to combine various search criteria and options to retrieve exactly the messages you need.
106+
107+
For example, you can easily fetch all messages in a folder:
108+
103109
```php
104110
$inbox = $mailbox->folders()->inbox();
105111

106-
// Get all the folder's messages.
112+
// Get all message UIDs in the inbox.
107113
$messages = $inbox->messages()->get();
108114

109-
// Get all the folder's messages with their bodies.
110-
$messages = $inbox->messages()->withBody()->get();
115+
// Get all messages in the inbox with various content.
116+
$messages = $inbox->messages()
117+
->withHeaders() // Enable fetching message headers.
118+
->withFlags() // Enable fetching message flags.
119+
->withBody() // Enable fetching message bodies.
120+
->get();
121+
```
122+
123+
> [!important]
124+
> It's paramount to understand that the `get()` method fetches all messages in the folder, and will be slow for large mailboxes.
125+
> When working with large mailboxes, consider using since(), and other criteria filters to limit the number of messages
126+
> your IMAP server returns, along with pagination or chunking to avoid memory issues.
127+
>
128+
> A typical approach when dealing with large mailboxes is to store all messages (either in a cache or DB)
129+
> once, and then only fetch new messages since the last time the mailbox was checked.
130+
131+
##### Filtering By Criteria
132+
133+
The MessageQuery supports many common IMAP search criteria. You can chain methods such as:
134+
135+
- `all()`
136+
- `new()`
137+
- `not()`
138+
- `old()`
139+
- `seen()`
140+
- `recent()`
141+
- `unseen()`
142+
- `deleted()`
143+
- `on($date)`
144+
- `uid($uid)`
145+
- `answered()`
146+
- `cc($value)`
147+
- `to($value)`
148+
- `bcc($value)`
149+
- `undeleted()`
150+
- `unflagged()`
151+
- `body($value)`
152+
- `from($email)`
153+
- `since($date)`
154+
- `text($value)`
155+
- `unanswered()`
156+
- `before($date)`
157+
- `flagged($value)`
158+
- `keyword($value)`
159+
- `subject($value)`
160+
- `unkeyword($value)`
161+
- `messageId($messageId)`
162+
- `inReplyTo($messageId)`
163+
- `language($countryCode)`
164+
- `header($header, $value)`
165+
166+
For example, to retrieve messages from the last 7 days with a specific subject:
167+
168+
```php
169+
$messages = $inbox->messages()
170+
->since(now()->subDays(7))
171+
->subject('Hello World')
172+
->get();
173+
```
174+
175+
If a method doesn't exist for a specific search criteria, you can use the `where()` method to add custom criteria:
176+
177+
```php
178+
$messages = $inbox->messages()
179+
->where('CRITERIA', 'value')
180+
->get();
181+
```
182+
183+
##### Fetching Additional Message Data
184+
185+
You can control what parts of the message are fetched by enabling or disabling them on the query builder:
186+
187+
**Message Headers:**
188+
Use `withHeaders()` to include headers in the result, or `withoutHeaders()` to exclude them.
189+
190+
**Message Body:**
191+
Use `withBody()` to fetch the full body content, or `withoutBody()` to skip it.
192+
193+
**Message Flags:**
194+
Use `withFlags()` to retrieve flags, or `withoutFlags()` to omit them.
195+
196+
For example, to fetch messages with both their bodies, headers, and flags:
197+
198+
```php
199+
$messages = $inbox->messages()
200+
->withHeaders()
201+
->withFlags()
202+
->withBody()
203+
->get();
204+
```
111205

112-
// Get messages since a certain date.
113-
$messages = $inbox->messages()->since(now()->subDays(7))->get();
206+
The less data you fetch, the faster your query will be. Only fetch the data you need.
114207

115-
// Get messages with a certain subject.
116-
$messages = $inbox->messages()->subject('Hello World')->get();
208+
##### Message Pagination
117209

118-
// Listen for new messages on the inbox.
119-
$inbox->idle(function (Message $message) {
120-
// Handle the new message.
121-
});
210+
You can paginate messages using the `paginate()` method. This method accepts the number of messages to display per page:
211+
212+
> [!important]
213+
> IMAP does not support native pagination, as you would expect from a SQL database. Instead,
214+
> ImapEngine retrieves all UID's from the selected folder, takes the slice of the UID's
215+
> that corresponds to the current page, and fetches the messages for those UID's.
216+
217+
```php
218+
// Paginate messages with 10 messages per page.
219+
$paginatedMessages = $inbox->messages()->paginate(10);
220+
```
221+
222+
##### Message Chunking
223+
224+
If you need to process a large number of messages without loading them all at once, you can use the chunk() method:
225+
226+
```php
227+
$inbox->messages()->chunk(function ($chunk, $page) {
228+
foreach ($chunk as $message) {
229+
// Process each message in the current chunk.
230+
}
231+
}, 20); // Process 20 messages per chunk.
232+
```
233+
234+
##### Finding a Specific Message
235+
236+
You can retrieve a single message by its unique identifier using the `find()` method.
237+
238+
The method accepts an ID and an ImapFetchIdentifier (an enum) that specifies whether the ID is a UID or a message sequence number.
239+
240+
For example, to find a message by UID:
241+
242+
```php
243+
use DirectoryTree\ImapEngine\Connection\ImapFetchIdentifier;
244+
245+
$message = $inbox->messages()->find(12345, ImapFetchIdentifier::Uid);
246+
```
247+
248+
Or by message sequence number:
249+
250+
```php
251+
$message = $inbox->messages()->find(1, ImapFetchIdentifier::MessageNumber);
122252
```

0 commit comments

Comments
 (0)