dg/imap

Retrieval, processing, and manipulation of emails within a mailbox via POP3, IMAP and NNTP

v1.0.0 2023-11-29 15:49 UTC

This package is auto-updated.

Last update: 2024-10-24 15:08:22 UTC


README

Downloads this Month Tests Latest Stable Version License

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

Installation

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.

Connecting to a Mailbox

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}',
	'your_username@gmai.com',
	'your_password',
);

$mailbox->connect();

Fetching Messages

Fetch all messages from the mailbox:

$messages = $mailbox->getMessages();
foreach ($messages as $message) {
	echo $message->getSubject() . "\n";
}

Handling Message Parts

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:

Error Handling

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();
}

Support Project

Do you like this project?

Donate