How can I check if an email folder exists using Zend_Mail_Storage_Imap, theres a createFOlder, renameFOlder and removeFolder and also a getFOlders but not exactly any fixed method to query if a certain mail folder exists? The GetFOlders returns a humonogous tree of folders to start with.
+1
A:
I've not worked with Zend_Mail_Storage_Imap
before, but from what I can glean from the source, this should do the trick:
/**
* Checks if a folder exists by name.
* @param Zend_Mail_Storage_Imap $imapObj Our IMAP object.
* @param string $folder The name of the folder to check for.
* @return boolean True if the folder exists, false otherwise.
*/
function folderExists(Zend_Mail_Storage_Imap $imapObj, $folder) {
try {
$imapObj->selectFolder($folder);
} catch (Zend_Mail_Storage_Exception $e) {
return false;
}
return true;
}
If you want to preserve the current folder through the check, it gets a bit trickier, of course:
/**
* Checks if a folder exists by name.
* @param Zend_Mail_Storage_Imap $imapObj Our IMAP object.
* @param string $folder The name of the folder to check for.
* @return boolean True if the folder exists, false otherwise.
* @throws Zend_Mail_Storage_Exception if the current folder cannot be restored.
*/
function folderExists(Zend_Mail_Storage_Imap $imapObj, $folder) {
$result = true;
$oldFolder = $imapObj->getCurrentFolder();
try {
$imapObj->selectFolder($folder);
} catch (Zend_Mail_Storage_Exception $e) {
$result = false;
}
$imapObj->selectFolder($oldFolder);
return $result;
}
(Your preference of how to deal with that situation may, of course, vary, that one is rather sloppy.)
pinkgothic
2010-04-09 18:40:39