PHP code example of sazanof / php-imap-sockets

1. Go to this page and download the library: Download sazanof/php-imap-sockets library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sazanof / php-imap-sockets example snippets


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();
bash
  composer