Skip to content

Commit 245e465

Browse files
committed
Update readme.md
1 parent fa447f3 commit 245e465

File tree

1 file changed

+125
-4
lines changed

1 file changed

+125
-4
lines changed

readme.md

Lines changed: 125 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,18 @@ $messages = $inbox->messages()->get();
116116
$messages = $inbox->messages()
117117
->withHeaders() // Enable fetching message headers.
118118
->withFlags() // Enable fetching message flags.
119-
->withBody() // Enable fetching message bodies.
119+
->withBody() // Enable fetching message bodies (including attachments).
120120
->get();
121121
```
122122

123123
> [!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.
124+
> It's paramount to understand that, without any query criteria specified, the `get()` method fetches all messages
125+
> in the folder by default, and will be slow for large mailboxes. When working with large mailboxes, consider
126+
> using `since($date)`, and other criteria filters to limit the number of messages your IMAP server returns,
127+
> along with pagination or chunking to avoid possible memory issues.
128+
>
129+
> You may also consider restricting the parts of the message you fetch using `withHeaders()`, `withFlags()`, and `withBody()`.
130+
> This will reduce the amount of data fetched from the server, and speed up your queries.
127131
>
128132
> A typical approach when dealing with large mailboxes is to store all messages (either in a cache or DB)
129133
> once, and then only fetch new messages since the last time the mailbox was checked.
@@ -190,6 +194,10 @@ Use `withHeaders()` to include headers in the result, or `withoutHeaders()` to e
190194
**Message Body:**
191195
Use `withBody()` to fetch the full body content, or `withoutBody()` to skip it.
192196

197+
> [!important]
198+
> The `withBody()` method fetches the full body content of the message, including attachments.
199+
> Keep this in mind when fetching messages, as it can be slow for large messages.
200+
193201
**Message Flags:**
194202
Use `withFlags()` to retrieve flags, or `withoutFlags()` to omit them.
195203

@@ -250,3 +258,116 @@ Or by message sequence number:
250258
```php
251259
$message = $inbox->messages()->find(1, ImapFetchIdentifier::MessageNumber);
252260
```
261+
262+
#### Interacting With Messages
263+
264+
Once you retrieve messages from a folder using methods like `$inbox->messages()->get()`, you'll receive instances of the `Message` class.
265+
266+
This class offers a rich set of helper methods for interacting with individual emails, making it easy to inspect, modify, and manipulate messages.
267+
268+
##### Retrieving Message Information
269+
270+
The `Message` class provides several methods to access basic properties:
271+
272+
- **UID and Flags**
273+
- `uid()`: Returns the unique identifier (UID) of the message.
274+
- `flags()`: Returns an array of flags currently set on the message.
275+
276+
- **Headers and Contents**
277+
- `headers()`: Returns the raw headers as a string.
278+
- `contents()`: Returns the raw message content.
279+
- `hasHeaders()` / `hasContents()`: Determine whether the message has headers or contents.
280+
281+
- **Metadata**
282+
- `subject()`: Returns the subject of the message.
283+
- `date()`: Returns the message’s date as a Carbon instance (if available).
284+
- `messageId()`: Retrieves the Message-ID header (globally unique identifier for the message).
285+
286+
##### Address Handling
287+
288+
To conveniently work with email addresses, the `Message` class includes methods that return addresses as instances of the `Address` class:
289+
290+
- `from()`: The sender’s address.
291+
- `sender()`: The actual sender (if different from "from").
292+
- `replyTo()`: The reply-to address.
293+
- `inReplyTo()`: The In-Reply-To address.
294+
- `to()`: An array of recipient addresses.
295+
- `cc()`: An array of CC addresses.
296+
- `bcc()`: An array of BCC addresses.
297+
298+
##### Content Retrieval
299+
300+
For accessing the message content in different formats:
301+
302+
- `html()`: Returns the HTML version of the message (if available).
303+
- `text()`: Returns the plain text version of the message (if available).
304+
305+
##### Attachment Handling
306+
307+
Messages that include attachments can be inspected with:
308+
309+
- `attachments()`: Returns an array of `Attachment` objects.
310+
- `hasAttachments()`: Checks if the message contains any attachments.
311+
- `attachmentCount()`: Returns the number of attachments in the message.
312+
313+
##### Flag Operations
314+
315+
The class also provides methods to modify message flags, which help you manage the state of a message:
316+
317+
- **Marking as Seen/Unseen**
318+
- `markSeen($expunge = true)`: Marks the message as read.
319+
- `unmarkSeen($expunge = true)`: Marks the message as unread.
320+
- *Aliases:* `markRead()` and `markUnread()`.
321+
322+
- **Other Flags**
323+
- `markAnswered()` / `unmarkAnswered()`
324+
- `markFlagged()` / `unmarkFlagged()`
325+
- `markDeleted()` / `unmarkDeleted()`
326+
- `markDraft()` / `unmarkDraft()`
327+
- `markRecent()` / `unmarkRecent()`
328+
329+
All these methods work by invoking the underlying IMAP `STORE` command (with the appropriate flag and operation), and optionally expunging the folder afterward.
330+
331+
##### Message Manipulation
332+
333+
Beyond just flagging, you can move or copy messages between folders, or even delete them:
334+
335+
- `copy(string $folder, bool $expunge = true)`: Copies the message to the specified folder.
336+
- `move(string $folder, bool $expunge = true)`: Moves the message to the specified folder.
337+
- `delete(bool $expunge = true)`: Marks the message as deleted and, if desired, expunges it from the folder.
338+
339+
##### Parsing and String Conversion
340+
341+
- `parse()`: Parses the raw message data into a `MailMimeMessage` instance for deeper inspection (e.g., extracting structured content, attachments, etc.).
342+
> **Note:** An exception is thrown if both headers and contents are empty.
343+
- `__toString()`: Converts the message back to its full raw string (headers and contents combined), which is useful for logging or re-sending the email.
344+
345+
---
346+
347+
##### Example: Interacting with a Retrieved Message
348+
349+
```php
350+
// Retrieve the first message from the inbox.
351+
$message = $inbox->messages()->get()->first();
352+
353+
// Print basic information.
354+
echo 'UID: ' . $message->uid() . PHP_EOL;
355+
echo 'Subject: ' . $message->subject() . PHP_EOL;
356+
echo 'Date: ' . ($message->date() ? $message->date()->toDateTimeString() : 'N/A') . PHP_EOL;
357+
358+
// Check if the message has attachments and list them.
359+
if ($message->hasAttachments()) {
360+
foreach ($message->attachments() as $attachment) {
361+
echo 'Attachment: ' . $attachment->filename() . ' (' . $attachment->contentType() . ')' . PHP_EOL;
362+
}
363+
}
364+
365+
// Mark the message as read.
366+
$message->markSeen();
367+
368+
// Move the message to an "Archive" folder.
369+
$message->move('Archive');
370+
371+
// Delete the message.
372+
$message->delete();
373+
```

0 commit comments

Comments
 (0)