6.0.0-alpha12
7/2/25
Last Modified 12/26/13 by Guest

Horde/Imap_Client usage examples

Assumptions

In these examples, $imap is assumed to be a previously configured/created Client object.

Get a list of messages in INBOX, since a certain date:

<?php

$query = new Horde_Imap_Client_Search_Query();
$query->dateSearch(
    new Horde_Date($date_string),
    Horde_Imap_Client_Search_Query::DATE_SINCE
);

// $list is an array, containing a single entry: 'match'
$results = $imap->search('INBOX', $query);

// A Horde_Imap_Client_Ids object containing the list of UIDs that matched the query.
var_dump($results['match']);
?>

Using the above message ids, fetch the plaintext body of the first email:

<?php

$query = new Horde_Imap_Client_Fetch_Query();
$query->structure();

$uid = new Horde_Imap_Client_Ids($results['match']->ids[0]);

$list = $imap->fetch('INBOX', $query, array(
    'ids' => $uid
));

$part = $list->first()->getStructure();
$id = $part->findBody();
$body = $part->getPart($id);

$query2 = new Horde_Imap_Client_Fetch_Query();
$query2->bodyPart($id, array(
    'decode' => true,
    'peek' => true
));

$list2 = $imap->fetch('INBOX', $query2, array(
    'ids' => $uid
));

$message2 = $list2->first();
$text = $message2->getBodyPart($id);
if (!$message2->getBodyPartDecode($id)) {
    // Quick way to transfer decode contents
    $body->setContents($text);
    $text = $body->getContents();
}

// $text now contains the plaintext body of the message.
?>