This IMAP Library for PHP provides an intuitive and easy-to-use interface to interact with POP3, IMAP and NNTP mail servers. It allows for operations such as connecting to mailboxes, fetching messages, handling message parts, and managing email content.
- Connect to IMAP servers with ease
- Fetch and manage email messages
- Handle different parts of an email such as attachments and text
- Decode email content and headers
- Supports message deletion and structure analysis
To install the library, you can use Composer. Run the following command in your project directory:
composer require dg/imap
It requires PHP version 8.1 with extension imap.
To connect to an IMAP mailbox, create an instance of the Mailbox
class.
use DG\Imap\Mailbox;
$mailbox = new Mailbox(
'{imap.gmail.com:993/imap/ssl}',
'[email protected]',
'your_password',
);
$mailbox->connect();
Fetch all messages from the mailbox:
$messages = $mailbox->getMessages();
foreach ($messages as $message) {
echo $message->getSubject() . "\n";
}
To handle different parts of a message, such as attachments:
foreach ($messages as $message) {
$parts = $message->getParts();
foreach ($parts as $part) {
// Process each part
}
}
Certainly, here's the additional information regarding error handling through exceptions:
In case of any issue, such as a failure to connect to the IMAP server, fetch messages, or process message parts, the library will throw an DG\Imap\Exception
:
try {
$mailbox->connect();
$messages = $mailbox->getMessages();
// ... additional operations
} catch (DG\Imap\Exception $e) {
echo 'Error: ' . $e->getMessage();
}
Do you like this project?