[commits] [Wiki] created: Doc/Dev/Imap_Client/Examples

Michael Slusarz slusarz at horde.org
Tue Jun 18 00:40:21 UTC 2013


slusarz  Mon, 17 Jun 2013 18:40:21 -0600

Created page: http://wiki.horde.org/Doc/Dev/Imap_Client/Examples

+ 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:
<code type="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']);
</code>

+++ Using the above message ids, fetch the plaintext body of the first email:
<code type="php">
$query = new Horde_Imap_Client_Fetch_Query();
$query->structure();

$uid = new Horde_Imap_Client_Ids($results['match'][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.
</code>



More information about the commits mailing list