ERROR: Could not complete request.
  Reason given: $message

"; exit; } else if ($response == "BAD") { echo "ERROR: Bad or malformed request.
  Server responded: $message

"; exit; } } return $data; } /** Parse the incoming mailbox name and return a string that is the FOLDER.MAILBOX **/ function findMailboxName($mailbox) { $mailbox = trim($mailbox); if (substr($mailbox, strlen($mailbox)-1, strlen($mailbox)) == "\"") { $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); $pos = strrpos($mailbox, "\"") + 1; $box = substr($mailbox, $pos, strlen($mailbox)); } else { $box = substr($mailbox, strrpos($mailbox, " ")+1, strlen($mailbox)); } return $box; } /** Finds the delimeter between mailboxes **/ function findMailboxDelimeter($imapConnection) { fputs($imapConnection, ". list \"\" \"\"\n"); $read = fgets($imapConnection, 1024); $pos = strrpos($read, "\""); $read = substr($read, 0, $pos); $pos = strrpos($read, "\""); $read = substr($read, 0, $pos); $pos = strrpos($read, "\""); $read = substr($read, 0, $pos); $pos = strrpos($read, "\""); $read = substr($read, $pos+1, strlen($read)); $tmp = fgets($imapConnection, 1024); return $read; } function getMailboxFlags($mailbox) { $mailbox = trim($mailbox); $mailbox = substr($mailbox, strpos($mailbox, "(")+1, strlen($mailbox)); $mailbox = substr($mailbox, 0, strpos($mailbox, ")")); $mailbox = str_replace("\\", "", $mailbox); $mailbox = strtolower($mailbox); $mailbox = explode(" ", $mailbox); return $mailbox; } // handles logging onto an imap server. function loginToImapServer($username, $key, $imapServerAddress, $hide) { require("../config/config.php"); $imapConnection = fsockopen($imapServerAddress, 143, &$errorNumber, &$errorString); if (!$imapConnection) { echo "Error connecting to IMAP Server.
"; echo "$errorNumber : $errorString
"; exit; } $serverInfo = fgets($imapConnection, 256); // login fputs($imapConnection, "a001 LOGIN $username $key\n"); $read = fgets($imapConnection, 1024); if ($debug_login == true) { echo "SERVER SAYS: $read
"; } if (substr($read, 0, 7) != "a001 OK") { if (!$hide) { if (substr($read, 0, 8) == "a001 BAD") { echo "Bad request: $read
"; exit; } else if (substr($read, 0, 7) == "a001 NO") { echo "
"; echo ""; echo " "; echo " "; echo "
"; echo "
ERROR
"; echo "
"; echo "

Unknown user or password incorrect.
Click here to try again.
"; echo "
"; echo ""; exit; } else { echo "Unknown error: $read
"; exit; } } else { exit; } } return $imapConnection; } /** must be sent in the form: user.. **/ function createFolder($imapConnection, $folder, $type) { require ("../config/config.php"); if (strtolower($type) == "noselect") { $dm = findMailboxDelimeter($imapConnection); $folder = "$folder$dm"; } else { $folder = "$folder"; } fputs($imapConnection, "1 create \"$folder\"\n"); $data = imapReadData($imapConnection, "1", false, $response, $message); if ($response == "NO") { echo "ERROR: Could not complete request.
  Reason given: $message

"; echo "Possible solutions:
  • You may need to specify that the folder is a subfolder of INBOX
  • "; exit; } else if ($response == "BAD") { echo "ERROR: Bad or malformed request.
      Server responded: $message

    "; exit; } } function removeFolder($imapConnection, $folder) { fputs($imapConnection, "1 delete \"$folder\"\n"); $data = imapReadData($imapConnection, "1", false, $response, $message); if ($response == "NO") { echo "ERROR: Could not delete the folder $folder."; echo "Probable causes:
    "; echo "
  • This folder may contain subfolders. Delete all subfolders first
  • "; exit; } else if ($response == "BAD") { echo "ERROR: Bad or malformed request.
      Server responded: $message

    "; exit; } } /** Sends back two arrays, boxesFormatted and boxesUnformatted **/ function getFolderList($imapConnection, &$boxesFormatted, &$boxesUnformatted, &$boxesRaw) { fputs($imapConnection, "1 list \"\" *\n"); $str = imapReadData($imapConnection, "1", true, $response, $message); $dm = findMailboxDelimeter($imapConnection); for ($i = 0;$i < count($str); $i++) { $mailbox = chop($str[$i]); $boxesRaw[$i] = $mailbox; $mailbox = findMailboxName($mailbox); $periodCount = countCharInString($mailbox, $dm); // indent the correct number of spaces. for ($j = 0;$j < $periodCount;$j++) $boxesFormatted[$i] = "$boxesFormatted[$i]  "; $boxesFormatted[$i] = $boxesFormatted[$i] . readShortMailboxName($mailbox, $dm); $boxesUnformatted[$i] = $mailbox; } } function deleteMessages($imapConnection, $a, $b, $numMessages, $trash_folder, $move_to_trash, $auto_expunge, $mailbox) { /** check if they would like to move it to the trash folder or not */ if ($move_to_trash == true) { $success = copyMessages($imapConnection, $a, $b, $trash_folder); if ($success == true) setMessageFlag($imapConnection, $a, $b, "Deleted"); } else { setMessageFlag($imapConnection, $a, $b, "Deleted"); } } function stripComments($line) { if (strpos($line, ";")) { $line = substr($line, 0, strpos($line, ";")); } if (strpos($line, "(") && strpos($line, ")")) { $full_line = $full_line . substr($line, 0, strpos($line, "(")); $full_line = $full_line . substr($line, strpos($line, ")")+1, strlen($line) - strpos($line, ")")); } else { $full_line = $line; } return $full_line; } ?>