This package is abandoned and no longer maintained. No replacement package was suggested.

A PHP wrapper class for PHP's IMAP-related email handling functions.

dev-master 2018-07-05 06:56 UTC

This package is auto-updated.

Last update: 2022-02-01 13:13:31 UTC


README

This library is an improvement of Jeff Geerling's project tailored specifically for my needs at the moment.

Keep in mind that this is still a work in progress so radical changes may occurr in time.

Installation

You can install this library via composer by running the command bellow or you can clone the repository.

composer require bigpaulie/imap

Usage

A mailbox in the context of this library is referring to a directory of your email account.

Connect to an IMAP account by creating a new Imap object with the required parameters:

$host = 'imap.example.com';
$user = 'johndoe';
$pass = '12345';
$port = 993;
$ssl = true;
$folder = 'INBOX';
$server = new Imap($host, $user, $pass, $port, $ssl, $folder);
Obtain a mailbox object
/** @var Mailbox $mailbox */
$mailbox = $server->getMailbox();

If you want to access another directory

/** @var Mailbox $mailbox */
$mailbox = $server->getMailbox('SPAM');
Mailbox info
/** @var array $info */
$info = $mailbox->getInfo();
Obtain a list of messages within a specific mailbox
/** @var Message[] $messages */
$messages = $mailbox->getMessages();

You can then iterate through the array of messages

/** @var Message $message */
foreach ($messages as $message) {
    $subject = $message->getSubject();
    $messageBody = $message->getBody();
}
Create a search criteria

You can obtain only certain messages if you want to.

Only undeleted messages

/** @var Search $criteria */
$criteria = new Search();
$criteria->setCriteria(Search::UNDELETED);

/** @var Message[] $messages */
$messages = $mailbox->getMessages($criteria);

You can add multiple search criterias for example :

/** @var Search $criteria */
$criteria = new Search();
$criteria->setCriteria(Search::UNDELETED);
$criteria->setCriteria(Search::FROM, "John Doe");
$criteria->setCriteria(Search::KEYWORD, "candy");

/** @var Message[] $messages */
$messages = $mailbox->getMessages($criteria);

Search criterias can be chained together:

/** @var Search $criteria */
$criteria = new Search();
$criteria->setCriteria(Search::UNDELETED)
    ->setCriteria(Search::FROM, "John Doe")
    ->setCriteria(Search::KEYWORD, "candy");

/** @var Message[] $messages */
$messages = $mailbox->getMessages($criteria);
Moving messages

You can move messages from one mailbox to another very easily

if ($mailbox->moveMessage($message, 'SPAM')) {
    echo "Message moved successfuly";
}
Copying messages

Copying messages is as easy as moving them

if ($mailbox->copyMessage($message, 'SPAM')) {
    echo "Message moved successfuly";
}
Deleting messages
$mailbox->deleteMessage($message);
Disconnecting from the server
$server->disconnect();

Contributions

If you want to make a contribution and improve the library or you noticed a bug you are more than welcome to do so.

For contributors just fork, code and submit a pull request.

Please maintain the coding style and testing patterns.