mailbox.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?
  2. /**
  3. ** mailbox.php
  4. **
  5. ** This contains functions that request information about a mailbox. Including
  6. ** reading and parsing headers, getting folder information, etc.
  7. **
  8. **/
  9. function selectMailbox($imapConnection, $mailbox, &$numberOfMessages) {
  10. // select mailbox
  11. fputs($imapConnection, "mailboxSelect SELECT \"$mailbox\"\n");
  12. $read = fgets($imapConnection, 1024);
  13. while ((substr($read, 0, 16) != "mailboxSelect OK") && (substr($read, 0, 17) != "mailboxSelect BAD")) {
  14. if (substr(Chop($read), -6) == "EXISTS") {
  15. $array = explode(" ", $read);
  16. $numberOfMessages = $array[1];
  17. }
  18. $read = fgets($imapConnection, 1024);
  19. }
  20. }
  21. function getMessageHeaders($imapConnection, $i, &$from, &$subject, &$date) {
  22. fputs($imapConnection, "messageFetch FETCH $i:$i RFC822.HEADER.LINES (From Subject Date)\n");
  23. $read = fgets($imapConnection, 1024);
  24. /* I have to replace <> with [] because HTML uses <> as tags, thus not printing what's in <> */
  25. $read = ereg_replace("<", "[", $read);
  26. $read = ereg_replace(">", "]", $read);
  27. while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
  28. if (substr($read, 0, 5) == "From:") {
  29. $read = ereg_replace("<", "EMAILSTART--", $read);
  30. $read = ereg_replace(">", "--EMAILEND", $read);
  31. $from = substr($read, 5, strlen($read) - 6);
  32. }
  33. else if (substr($read, 0, 5) == "Date:") {
  34. $read = ereg_replace("<", "[", $read);
  35. $read = ereg_replace(">", "]", $read);
  36. $date = substr($read, 5, strlen($read) - 6);
  37. }
  38. else if (substr($read, 0, 8) == "Subject:") {
  39. $read = ereg_replace("<", "[", $read);
  40. $read = ereg_replace(">", "]", $read);
  41. $subject = substr($read, 8, strlen($read) - 9);
  42. }
  43. $read = fgets($imapConnection, 1024);
  44. }
  45. }
  46. function getMessageFlags($imapConnection, $i, &$flags) {
  47. /** * 2 FETCH (FLAGS (\Answered \Seen)) */
  48. fputs($imapConnection, "messageFetch FETCH $i:$i FLAGS\n");
  49. while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
  50. if (strpos($read, "FLAGS")) {
  51. $read = ereg_replace("\(", "", $read);
  52. $read = ereg_replace("\)", "", $read);
  53. $read = substr($read, strpos($read, "FLAGS")+6, strlen($read));
  54. $read = trim($read);
  55. $flags = explode(" ", $read);;
  56. $s = 0;
  57. while ($s < count($flags)) {
  58. $flags[$s] = substr($flags[$s], 1, strlen($flags[$s]));
  59. $s++;
  60. }
  61. } else {
  62. $flags[0] = "None";
  63. }
  64. $read = fgets($imapConnection, 1024);
  65. }
  66. }
  67. function getEmailAddr($sender) {
  68. if (strpos($sender, "EMAILSTART--") == false)
  69. return "";
  70. $start = strpos($sender, "EMAILSTART--");
  71. $emailAddr = substr($sender, $start, strlen($sender));
  72. return $emailAddr;
  73. }
  74. function getSender($sender) {
  75. if (strpos($sender, "EMAILSTART--") == false)
  76. return "";
  77. $first = substr($sender, 0, strpos($sender, "EMAILSTART--"));
  78. $second = substr($sender, strpos($sender, "--EMAILEND") +10, strlen($sender));
  79. return "$first$second";
  80. }
  81. function getSenderName($sender) {
  82. $name = getSender($sender);
  83. $emailAddr = getEmailAddr($sender);
  84. $emailStart = strpos($emailAddr, "EMAILSTART--");
  85. $emailEnd = strpos($emailAddr, "--EMAILEND") - 10;
  86. if (($emailAddr == "") && ($name == "")) {
  87. $from = $sender;
  88. }
  89. else if ((strstr($name, "?") != false) || (strstr($name, "$") != false) || (strstr($name, "%") != false)){
  90. $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
  91. $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
  92. $from = $emailAddr;
  93. }
  94. else if (strlen($name) > 0) {
  95. $from = $name;
  96. }
  97. else if (strlen($emailAddr > 0)) {
  98. $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
  99. $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
  100. $from = $emailAddr;
  101. }
  102. $from = trim($from);
  103. // strip out any quotes if they exist
  104. if ((strlen($from) > 0) && ($from[0] == "\"") && ($from[strlen($from) - 1] == "\""))
  105. $from = substr($from, 1, strlen($from) - 2);
  106. return $from;
  107. }
  108. ?>