The PhpImap\Mailbox class is the primary entry point for interacting with mailboxes via POP3, IMAP, or NNTP.
Basic Initialization
When instantiating Mailbox, you can specify the server connection string, credentials, an attachment directory, and encoding.
Attachment Management
- Filename Mode: By default, attachments use random filenames to prevent overwriting. Set the last argument of the constructor to
true to use original filenames. - Collision Handling: To prevent overwriting existing files with the same name, use
setAttachmentFilenameCollisionMode with PhpImap\Mailbox::ATTACHMENT_FILENAME_COLLISION_SUFFIX to automatically append suffixes like (1), (2), etc. - Performance: If you do not need to download attachments, call
$mailbox->setAttachmentsIgnore(true) to significantly increase performance.
Searching and Retrieving
- Use
searchMailbox($criteria) to find email IDs. The criteria follow PHP's imap_search documentation. - Use
getMail($id) to retrieve a message object. To inspect a message without marking it as 'seen', use getMail($id, false).
Accessing Email Data
Once you have a mail object, you can check for attachments with hasAttachments(), retrieve them with getAttachments(), or access headers using getHeader($name) or getHeaders($name).