PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Checking for flags

Maildir and IMAP support storing flags. The class Zend_Mail_Storage has constants for all known maildir and IMAP system flags, named Zend_Mail_Storage::FLAG_<flagname>. To check for flags Zend_Mail_Message has a method called hasFlag(). With getFlags() you'll get all set flags.

<?php
// find unread messages
echo "Unread mails:\n";
foreach (
$mail as $message) {
    if (
$message->hasFlag(Zend_Mail_Storage::FLAG_SEEN)) {
        continue;
    }
    
// mark recent/new mails
    
if ($message->hasFlag(Zend_Mail_Storage::FLAG_RECENT)) {
        echo 
'! ';
    } else {
        echo 
'  ';
    }
    echo 
$message->subject "\n";
}

// check for known flags
$flags $message->getFlags();
echo 
"Message is flagged as: ";
foreach (
$flags as $flag) {
    switch (
$flag) {
        case 
Zend_Mail_Storage::FLAG_ANSWERED:
            echo 
'Answered ';
            break;
        case 
Zend_Mail_Storage::FLAG_FLAGGED:
            echo 
'Flagged ';
            break;

        
// ...
        // check for other flags
        // ...

        
default:
            echo 
$flag '(unknown flag) ';
    }
}

As IMAP allows user or client defined flags, you could get flags that don't have a constant in Zend_Mail_Storage. Instead, they are returned as strings and can be checked the same way with hasFlag().

<?php
// check message for client defined flags $IsSpam, $SpamTested
if (!$message->hasFlag('$SpamTested')) {
    echo 
'message has not been tested for spam';
} else if (
$message->hasFlag('$IsSpam')) {
    echo 
'this message is spam';
} else {
    echo 
'this message is ham';
}

Zend Framework