Sample code for 30+ languages & platforms
PHP Requires Chilkat v11.0.0+

Download MIME Source of Emails in IMAP Mailbox

Demonstrates how to download the MIME source for emails on an IMAP server.

Chilkat PHP Downloads

PHP
<?php

use Chilkat2\Imap;
use Chilkat2\StringBuilder;

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

$imap = new Imap();

// Connect to an IMAP server.
// Use TLS
$imap->Ssl = true;
$imap->Port = 993;
try {
    $imap->Connect('imap.example.com');

    // Login
    $imap->Login('myLogin', 'myPassword');

    // Select an IMAP mailbox
    $imap->SelectMailbox('Inbox');
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

// The NumMessages property contains the number of messages in the selected mailbox.
$numMessages = $imap->NumMessages;
if ($numMessages == 0) {
    echo "No messages exist in the Inbox.\n";
    exit;
}

$sbMime = new StringBuilder();
for ($seqNum = 1; $seqNum <= $numMessages; $seqNum++) {
    $sbMime->Clear();
    try {
        $imap->FetchSingleAsMimeSb($seqNum, false, $sbMime);
    } catch (\Chilkat2\Exception $e) {
        echo $e->getMessage(), "\n";
        exit;
    }

    // The MIME source of the downloaded email is contained in sbMime.
}

// Disconnect from the IMAP server.
$imap->Disconnect();