PHP code example of mlocati / imap

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

    

mlocati / imap example snippets






use MLocati\IMAP\Client;


$client = new Client(
	'MailboxLogin',
    'MailboxPassword',
    'imap.server.com',
    '/tls'
);

// List messages
foreach ($client->getMessages() as $message) {
    echo '#Subject: ', $message->getSubject(), "\n";
    if ($message->getDate() !== null) {
        echo ' Date: ', $message->getDate()->format('c'), "\n";
    }
    echo ' From: ', $message->getFrom(), "\n";
    echo ' To: ', $message->getTo(), "\n";
    echo ' Message ID: ', $message->getID(), "\n";
    echo ' Marked for deletion? ', $message->isDeleted() ? 'yes' : 'no', "\n";
    echo " Total message parts: ", count($message->getAllParts()), "\n";
    printPart($message->getRootPart(), 0);

    if ($message->getSubject() == 'Hi!') {
        $message->delete();
    } elseif ($message->isDeleted()) {
        $message->undelete();
    }
}

// Delete all messages marked for deletion.
$client->expunge();

function printPart(MLocati\IMAP\MessagePart $part, $deepLevel)
{
    $indent = str_repeat("  ", $deepLevel + 1);
    echo $indent, "#ContentType: ", $part->getFullType(), "\n";
    echo $indent, " Name: ", $part->getName(), "\n";
    echo $indent, " Description: ", $part->getDescription(), "\n";
    echo $indent, " Disposition: ", $part->getDisposition(), "\n";
    echo $indent, " DispositionName: ", $part->getDispositionName(), "\n";
    echo $indent, " Content: ", strlen($part->getContents()), " bytes\n";
    $children = $part->getParts();
    if (empty($children)) {
        echo $indent, " No child parts\n";
    } else {
        echo $indent, " ", count($children)," child part(s):\n";
        foreach ($children as $child) {
            printPart($child, $deepLevel + 1);
        }
    }
}