PHP code example of clivern / imap

1. Go to this page and download the library: Download clivern/imap 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/ */

    

clivern / imap example snippets


composer 



use Clivern\Imap\Core\Connection;

$connection = new Connection(
    "imap.gmail.com",
    "993",
    "[email protected]",
    "my_password",
    "/ssl",
    "INBOX"
);
$connection->connect();

$connection->disconnect();

$folder = "INBOX";

// Reconnect & Update Mailbox Folder
$connection->reconnect($folder);
// Reopen Connection
$connection->survive($folder);
// Get Connection Stream
$connection->getStream();
// Get Server String
$connection->getServer();
// Check Connection
$connection->checkConnection();
// Get Quota Array
$connection->getQuota($folder);
// Get Status Array
$connection->getStatus($folder);
// Check MailBox Data
$connection->check();
// Ping Connection
$connection->ping();
// Get Errors
$connection->getErrors();
// Get Alerts
$connection->getAlerts();
// Get Last Error
$connection->getLastError();
// Disconnect
$connection->disconnect();

use Clivern\Imap\MailBox;

$mailbox = new MailBox($connection);

$messages = $mailbox->getMessages();

foreach ($messages as $message) {
    echo "Subject: " . $message->header()->get('subject');
    echo "<br/>";
    echo $message->body()->getMessage();
}


use Clivern\Imap\Core\Search;
use Clivern\Imap\Core\Search\Condition\All;
use Clivern\Imap\Core\Search\Condition\Answered;
use Clivern\Imap\Core\Search\Condition\BCC;
use Clivern\Imap\Core\Search\Condition\Before;
use Clivern\Imap\Core\Search\Condition\Body;
use Clivern\Imap\Core\Search\Condition\CC;
use Clivern\Imap\Core\Search\Condition\Deleted;
use Clivern\Imap\Core\Search\Condition\Flagged;
use Clivern\Imap\Core\Search\Condition\From;
use Clivern\Imap\Core\Search\Condition\Keyword;
use Clivern\Imap\Core\Search\Condition\NewFlag;
use Clivern\Imap\Core\Search\Condition\Old;
use Clivern\Imap\Core\Search\Condition\On;
use Clivern\Imap\Core\Search\Condition\Recent;
use Clivern\Imap\Core\Search\Condition\Seen;
use Clivern\Imap\Core\Search\Condition\Since;
use Clivern\Imap\Core\Search\Condition\Subject;
use Clivern\Imap\Core\Search\Condition\Text;
use Clivern\Imap\Core\Search\Condition\To;
use Clivern\Imap\Core\Search\Condition\UnAnswered;
use Clivern\Imap\Core\Search\Condition\UnDeleted;
use Clivern\Imap\Core\Search\Condition\UnFlagged;
use Clivern\Imap\Core\Search\Condition\UnKeyword;
use Clivern\Imap\Core\Search\Condition\UnSeen;

$search = new Search();
$search->addCondition(new All());
// $search->addCondition(new Answered());
// $search->addCondition(new BCC("[email protected]"));
// $search->addCondition(new Before(date("j F Y")));
// $search->addCondition(new Body("search text"));
// $search->addCondition(new CC("[email protected]"));
// $search->addCondition(new Deleted());
// $search->addCondition(new Flagged());
// $search->addCondition(new From("[email protected]"));
// $search->addCondition(new Keyword("test"));
// $search->addCondition(new NewFlag());
// $search->addCondition(new Old());
// $search->addCondition(new On(date("j F Y")));
// $search->addCondition(new Recent());
// $search->addCondition(new Seen());
// $search->addCondition(new Since(date("j F Y")));
// $search->addCondition(new Subject("search text"));
// $search->addCondition(new Text("search text"));
// $search->addCondition(new To("[email protected]"));
// $search->addCondition(new UnAnswered());
// $search->addCondition(new UnDeleted());
// $search->addCondition(new UnFlagged());
// $search->addCondition(new UnKeyword("test"));
// $search->addCondition(new UnSeen());

// For more info, please check http://php.net/manual/en/function.imap-search.php

use Clivern\Imap\MailBox;

$mailbox = new MailBox($connection);

$messages = $mailbox->getMessages($search);

foreach ($messages as $message) {
    echo "Subject: " . $message->header()->get('subject');
    echo "<br/>";
    echo $message->body()->getMessage();
}

use Clivern\Imap\MailBox;

$mailbox = new MailBox($connection);
// Get Folders
$mailbox->getFolders();
// Update Folder
$mailbox->setFolder("[Gmail]/All Mail");
// Count Messages in Current Folder
$mailbox->count();

$messages = $mailbox->getMessages();

foreach ($messages as $message) {
    echo "Subject: " . $message->header()->get('subject');
    echo "<br/>";
    echo $message->body()->getMessage();
}

$message->header()->get('subject');
$message->header()->get('from');
$message->header()->get('to');
$message->header()->get('date');
$message->header()->get('message_id');
$message->header()->get('in_reply_to');
$message->header()->get('references');
$message->header()->get('size');
$message->header()->get('uid');
$message->header()->get('msgno');
$message->header()->get('recent');
$message->header()->get('flagged');
$message->header()->get('answered');
$message->header()->get('deleted');
$message->header()->get('seen');
$message->header()->get('draft');
$message->header()->get('udate');

$message->body()->getMessage();
$message->body()->getEncoding();

$attachments = $message->attachments();
foreach ($attachments as $attachment) {
    $attachment->getFilename();
    $attachment->getExtension();
    $attachment->getSize();
    $attachment->getEncoding();
    $attachment->getBytes();
    // get attachment content
    $attachment->getPlainBody();
    // get decoded attachment content
    $attachment->getBody();
    // Store attachment in provided path
    $attachment->store(__DIR__ . '/');
}

$message->action()->delete();
$message->action()->undelete();
// and don't forget to run the following to delete all messages marked for deletion
$mailbox->expunge();