5 ** This contains functions that request information about a mailbox. Including
6 ** reading and parsing headers, getting folder information, etc.
10 function selectMailbox($imapConnection, $mailbox, &$numberOfMessages) {
12 fputs($imapConnection, "mailboxSelect SELECT \"$mailbox\"\n");
13 $read = fgets($imapConnection, 1024);
14 while ((substr($read, 0, 16) != "mailboxSelect OK") && (substr($read, 0, 17) != "mailboxSelect BAD")) {
15 if (substr(Chop($read), -6) == "EXISTS") {
16 $array = explode(" ", $read);
17 $numberOfMessages = $array[1];
19 $read = fgets($imapConnection, 1024);
23 function getMessageHeaders($imapConnection, $i, &$from, &$subject, &$date) {
24 fputs($imapConnection, "messageFetch FETCH $i:$i RFC822.HEADER.LINES (From Subject Date)\n");
25 $read = fgets($imapConnection, 1024);
26 /* I have to replace <> with [] because HTML uses <> as tags, thus not printing what's in <> */
27 $read = ereg_replace("<", "[", $read);
28 $read = ereg_replace(">", "]", $read);
30 while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
31 if (substr($read, 0, 5) == "From:") {
32 $read = ereg_replace("<", "EMAILSTART--", $read);
33 $read = ereg_replace(">", "--EMAILEND", $read);
34 $from = substr($read, 5, strlen($read) - 6);
36 else if (substr($read, 0, 5) == "Date:") {
37 $read = ereg_replace("<", "[", $read);
38 $read = ereg_replace(">", "]", $read);
39 $date = substr($read, 5, strlen($read) - 6);
41 else if (substr($read, 0, 8) == "Subject:") {
42 $read = ereg_replace("<", "[", $read);
43 $read = ereg_replace(">", "]", $read);
44 $subject = substr($read, 8, strlen($read) - 9);
47 $read = fgets($imapConnection, 1024);
51 function getMessageFlags($imapConnection, $i, &$flags) {
52 /** * 2 FETCH (FLAGS (\Answered \Seen)) */
53 fputs($imapConnection, "messageFetch FETCH $i:$i FLAGS\n");
54 while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
55 if (strpos($read, "FLAGS")) {
56 $read = ereg_replace("\(", "", $read);
57 $read = ereg_replace("\)", "", $read);
58 $read = substr($read, strpos($read, "FLAGS")+
6, strlen($read));
60 $flags = explode(" ", $read);;
62 while ($i < count($flags)) {
63 $flags[$i] = substr($flags[$i], 1, strlen($flags[$i]));
69 $read = fgets($imapConnection, 1024);
73 function getEmailAddr($sender) {
74 if (strpos($sender, "EMAILSTART--") == false)
77 $start = strpos($sender, "EMAILSTART--");
78 $emailAddr = substr($sender, $start, strlen($sender));
83 function getSender($sender) {
84 if (strpos($sender, "EMAILSTART--") == false)
87 $first = substr($sender, 0, strpos($sender, "EMAILSTART--"));
88 $second = substr($sender, strpos($sender, "--EMAILEND") +
10, strlen($sender));
89 return "$first$second";
92 function getSenderName($sender) {
93 $name = getSender($sender);
94 $emailAddr = getEmailAddr($sender);
95 $emailStart = strpos($emailAddr, "EMAILSTART--");
96 $emailEnd = strpos($emailAddr, "--EMAILEND") - 10;
98 if (($emailAddr == "") && ($name == "")) {
101 else if ((strstr($name, "?") != false) ||
(strstr($name, "$") != false) ||
(strstr($name, "%") != false)){
102 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
103 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
106 else if (strlen($name) > 0) {
109 else if (strlen($emailAddr > 0)) {
110 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
111 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
117 // strip out any quotes if they exist
118 if ((strlen($from) > 0) && ($from[0] == "\"") && ($from[strlen($from) - 1] == "\""))
119 $from = substr($from, 1, strlen($from) - 2);