Check if a mail folder exists using Zend Mail

How to check if a mail folder exists using Zend_Mail_Storage_Imap, createFOlder, renameFOlder and removeFolder, as well as getFOlders, but not exactly any fixed method for querying if a specific mail folder exists? GetFOlders returns a tree with a humological tree to begin with.

+3
source share
1 answer

I haven't worked with before Zend_Mail_Storage_Imap, but from what I can extract 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 save the current folder through a check, this is, of course, a little more complicated:

/**
 * 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;
}

( , , , , , .)

+1

Source: https://habr.com/ru/post/1740131/


All Articles