NEW library for working with email, using sockets.
- Dependency free
- Body structure analyze via [BODYSTRUCTURE] command
- Flexible search and fetch
- Attachments support (inline too)
- Flags management
- Pagination
- Sorting
To install this project run
composer require sazanof/php-imap-sockets
use \Sazanof\PhpImapSockets\Models\Connection;
// create new connection
$connection = new Connection('imap.example.com');
// open connection and enable debug
$connection->open()->enableDebug();
// login
$connection->login('USERNAME', 'PASSWORD');
use \Sazanof\PhpImapSockets\Models\SearchQuery;
$query = new SearchQuery();
$query->all();
// OR
$query->subject('Test');
// OR
$query->new();
// OR
$query->or([
'subject'=>[
'One',
'Two'
],
'since'=>'01-Jan-2023'
]);
// Use clear() method to clear query string
$query->clear();
$path = 'INBOX';
$mailbox = $connection->getMailboxByPath($path)->setConnection($connection)->select();
// array of messages NUMBERS (not UIDS)
$uids = $mailbox->search($query)->msgNums();
// or
$mailbox->search($query)->setOrderDirection('DESC')->msgNums() // or ASC
use \Sazanof\PhpImapSockets\Models\MessageCollection;
$collection = new MessageCollection($uids, $mailbox);
// array of "Message"
$items = $collection->items();
use \Sazanof\PhpImapSockets\Models\Paginator;
$paginator = new \Sazanof\PhpImapSockets\Models\Paginator($uids, $mailbox, 1, 6);
$messagesPaginated = $p->messages();
/** @var \Sazanof\PhpImapSockets\Models\Message $message **/
$attachmentsParts = $message->getBodyStructure()->getAttachmentParts();
foreach ($attachmentsParts as $attachmentsPart) {
if (!$attachmentsPart->isInline()) {
// set attachment content to $attachmentsPart
$attachmentsPart->setContent(
$message->getAttachment($attachmentsPart->getSection()) // or save locally
);
}
}
/** @var \Sazanof\PhpImapSockets\Models\Message $message **/
//get text parts (plain,html) with inline images
$message->getBodyStructure()->getTextParts();
use \Sazanof\PhpImapSockets\Models\Message;
//$message
$message->setImportant()->markAsDeleted();
//or
$message->addFlags(['one','two'])
//or
$message->replaceFlags(['one','two'])
//or
$message->clearFlags()
//or
$message->deleteFlags('one');
//trigger save
$message->saveFlags();
If you detected any security issues, please reach out to us at m@sazanof.ru