[dev] Opening E-Mails via IMP

Thomas Jarosch thomas.jarosch at intra2net.com
Mon Sep 16 07:33:49 UTC 2019


Hi Sebastian,

You wrote on Sun, Sep 15, 2019 at 11:47:05AM +0200:
> Thanks Michael, that looks promising.
> 
> Pardon my ignorance but where do I go from there? The API gives me a
> Horde_Imap_Client_Base object which I probably use to get a
> Horde_Imap_Client_Mailbox, OK.  But I cannot see how to access a message
> from there. What am I missing?

here's a stripped down version of a script I use to walk all emails in all 
IMAP mailboxes.

Basic procedure:
* Connect to IMAP server
* List all mailboxes
* Walk each folder
  * Walk each message
    * Get a bunch of headers

Note: It doesn't run, I just kept all the relevant Horde_Imap calls
in there as an API example:

<?php

define("HORDE_BASE", "/usr/share/pear/Horde");
require_once HORDE_BASE . "/Autoloader/Default.php";

function collector_get_headers($message)
{

    return($headers);
}

$imap = new Horde_Imap_Client_Socket(array("username" => $username, "password" => $password, "hostspec" => COLLECTOR_HOST, "port" => COLLECTOR_PORT, "secure" => false));

// Determine IMAP delimiter
$namespaces = $imap->getNamespaces();
$last_namespace = array_pop($namespaces);
$imap_delimiter = $last_namespace['delimiter'];

// --- Get the list of folders ("mailboxes" in IMAP speak) in that user's account from the server --------------------

$folders = $imap->listMailboxes("*", Horde_Imap_Client::MBOX_ALL, array("flat" => true));

// --- Walk through the list of folders --------------------
foreach($folders as $folder)
{
    $status = $imap->status($folder, Horde_Imap_Client::STATUS_MESSAGES | Horde_Imap_Client::STATUS_UIDNEXT_FORCE | Horde_Imap_Client::STATUS_UIDVALIDITY);

    echo "    Current status: MESSAGES=" . $status["messages"] . ", UIDNEXT=" . $status["uidnext"] . ", UIDVALIDITY=" . $status["uidvalidity"] . "\n";

    // --- Get a list of all message UIDs in the folder from the server --------------------

    $query = new Horde_Imap_Client_Fetch_Query;
    $query->uid();
    $query->headerText(array("peek" => true));
    $messages = $imap->fetch($folder, $query);

    if($messages->key_type != Horde_Imap_Client_Fetch_Results::UID)
        collector_error("Please make sure the ids() method of Horde_Imap_Client_Fetch_Results returns UIDs instead of sequence ids", true);

    $uids = $messages->ids();

    // --- Walk through the list of message UIDs --------------------

    foreach($uids as $uid)
    {
        $message = $messages->get($uid);

        $headers = array();
        $headers_all = $message->getHeaderText(0, Horde_Imap_Client_Data_Fetch::HEADER_PARSE);

        foreach(array("message-id", "date", "x-spam-level") as $header)
        {
            $headers[$header] = $headers_all->getValue($header);

            // pick first one if there are multiple values
            if(is_array($headers[$header]))
                $headers[$header] = $headers[$header][0];
        }
    }
}

?>

HTH,
Thomas


More information about the dev mailing list