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, $start, $end, &$from, &$subject, &$date) {
27 if (($start > $end) ||
($start < 1)) {
28 echo "Error in message header fetching. Start message: $start, End message: $end<BR>";
32 while ($rel_start <= $end) {
33 if ($end - $rel_start > 50) {
34 $rel_end = $rel_start +
50;
38 fputs($imapConnection, "messageFetch FETCH $rel_start:$rel_end RFC822.HEADER.LINES (From Subject Date)\n");
39 $read = fgets($imapConnection, 1024);
41 $from_num = $rel_start - 1;
42 $date_num = $rel_start - 1;
43 $subj_num = $rel_start - 1;
44 while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
45 if (substr($read, 0, 5) == "From:") {
46 $read = ereg_replace("<", "EMAILSTART--", $read);
47 $read = ereg_replace(">", "--EMAILEND", $read);
48 $from[$from_num] = substr($read, 5, strlen($read) - 6);
51 else if (substr($read, 0, 5) == "Date:") {
52 $read = ereg_replace("<", "[", $read);
53 $read = ereg_replace(">", "]", $read);
54 $date[$date_num] = substr($read, 5, strlen($read) - 6);
57 else if (substr($read, 0, 8) == "Subject:") {
58 $read = ereg_replace("<", "[", $read);
59 $read = ereg_replace(">", "]", $read);
60 $subject[$subj_num] = substr($read, 8, strlen($read) - 9);
63 $read = fgets($imapConnection, 1024);
65 $rel_start = $rel_start +
50;
69 function setMessageFlag($imapConnection, $i, $q, $flag) {
70 fputs($imapConnection, "messageStore STORE $i:$q +FLAGS (\\$flag)\n");
73 function getMessageFlags($imapConnection, $j, &$flags) {
74 /** * 2 FETCH (FLAGS (\Answered \Seen)) */
75 fputs($imapConnection, "messageFetch FETCH $j:$j FLAGS\n");
76 $read = fgets($imapConnection, 1024);
78 while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
79 if (strpos($read, "FLAGS")) {
80 $read = ereg_replace("\(", "", $read);
81 $read = ereg_replace("\)", "", $read);
82 $read = substr($read, strpos($read, "FLAGS")+
6, strlen($read));
84 $flags = explode(" ", $read);;
86 while ($s < count($flags)) {
87 $flags[$s] = substr($flags[$s], 1, strlen($flags[$s]));
94 $read = fgets($imapConnection, 1024);
98 function getEmailAddr($sender) {
99 if (strpos($sender, "EMAILSTART--") == false)
102 $start = strpos($sender, "EMAILSTART--");
103 $emailAddr = substr($sender, $start, strlen($sender));
108 function getSender($sender) {
109 if (strpos($sender, "EMAILSTART--") == false)
112 $first = substr($sender, 0, strpos($sender, "EMAILSTART--"));
113 $second = substr($sender, strpos($sender, "--EMAILEND") +
10, strlen($sender));
114 return "$first$second";
117 function getSenderName($sender) {
118 $name = getSender($sender);
119 $emailAddr = getEmailAddr($sender);
120 $emailStart = strpos($emailAddr, "EMAILSTART--");
121 $emailEnd = strpos($emailAddr, "--EMAILEND") - 10;
123 if (($emailAddr == "") && ($name == "")) {
126 else if ((strstr($name, "?") != false) ||
(strstr($name, "$") != false) ||
(strstr($name, "%") != false)){
127 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
128 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
131 else if (strlen($name) > 0) {
134 else if (strlen($emailAddr > 0)) {
135 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
136 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
142 // strip out any quotes if they exist
143 if ((strlen($from) > 0) && ($from[0] == "\"") && ($from[strlen($from) - 1] == "\""))
144 $from = substr($from, 1, strlen($from) - 2);
149 /** returns "true" if the copy was completed successfully.
150 ** returns "false" with an error message if unsuccessful.
152 function copyMessages($imapConnection, $from_id, $to_id, $folder) {
153 fputs($imapConnection, "mailboxStore COPY $from_id:$to_id \"$folder\"\n");
154 $read = fgets($imapConnection, 1024);
155 while ((substr($read, 0, 15) != "mailboxStore OK") && (substr($read, 0, 15) != "mailboxStore NO")) {
156 $read = fgets($imapConnection, 1024);
159 if (substr($read, 0, 15) == "mailboxStore NO") {
160 echo "ERROR... $read<BR>";
162 } else if (substr($read, 0, 15) == "mailboxStore OK") {
166 echo "UNKNOWN ERROR copying messages $from_id to $to_id to folder $folder.<BR>";
170 /** expunges a mailbox **/
171 function expungeBox($imapConnection, $mailbox) {
172 selectMailbox($imapConnection, $mailbox, $num);
173 fputs($imapConnection, "1 EXPUNGE\n");
176 function getFolderNameMinusINBOX($mailbox) {
177 if (substr($mailbox, 0, 6) == "INBOX.")
178 $box = substr($mailbox, 6, strlen($mailbox));