Hi, I am currently writting a little piece of code that will allow me to see all the emails sent TO or received FROM a client. The thing is these emails can be sppread over 2-3 IMAP accounts.
That said, I wrote the code to scan through 1 IMAP account. The problem I am having is that I did not find a way to just search through the whole account, instead I have to do it directory by directory, which takes awhile.
I was wondering what is the most efficient way to search through an entire IMAP account tree, hopefully without having to go through the directories manually.
Here is the first draft of my code:
$mbox = imap_open("{192.168.1.11:143/imap/notls}", "XX", "XXXX",OP_HALFOPEN)
// GET the list of directories and put them in an array
$list = imap_list($mbox, "{192.168.1.11}", "*");
if (is_array($list)) {
reset($list);
$liste_repertoire = array();
while (list($key, $val) = each($list))
array_push($liste_repertoire,imap_utf7_decode($val));
}
imap_close($mbox);
// Go through the directories
while ($repertoire = array_pop($liste_repertoire)) {
//Remove the leading {ip adress}
$i = strpos($repertoire,"}");
$r = substr_replace($repertoire,"",0,$i+1);
$mbox = imap_open("{192.168.1.11:143/imap/notls}$r", "XXX", "XXXX",OP_READONLY)
// Search through all the posibilities
$email = "XXX@XXX.com";
$result_to = imap_search($mbox, "TO \"".$email."\"", SE_UID);
$result_cc = imap_search($mbox, "CC \"".$email."\"", SE_UID);
$result_from = imap_search($mbox, "FROM \"".$email."\"", SE_UID);
// I simply put all the UIDs in a variable
$email_to_fetch = "";
if ($result_to)
foreach ($result_to as $key=>$value)
$email_to_fetch .= ",$value";
if ($result_cc)
foreach ($result_cc as $key=>$value)
$email_to_fetch .= ",$value";
if ($result_from)
foreach ($result_from as $key=>$value)
$email_to_fetch .= ",$value";
// Remove leading comma
$email_to_fetch = trim($email_to_fetch,",");
// Search
$email_fetch = imap_fetch_overview($mbox, $email_to_fetch,FT_UID);
// Print search results
while ($email = array_pop($email_fetch)) {
$email;
print $mail->from;
print $mail->to;
print $mail->subject;
print $mail->date;
print "<BR>";
}
imap_close($mbox);
}
========================
Thanks,
Jd
sorry couldn't get the tabs to work in the code