php imap - imap_setflag_full

I have this code it works perfectly.... the only problem i have is with imap_setflag_full($imap,$i, "\\Seen"); that the flag SEEN doesn't seem to be setting ..

 $imap = imap_open("{mail.xyz.com:993/imap/ssl/novalidate-cert}INBOX", "username", "pass");       
    $message_count=2;
        for ($i = 1; $i <= $message_count; ++$i) {
            $header = imap_header($imap, $i);
            $body = trim(substr(imap_body($imap, $i), 0, 1000));
            $prettydate = date("jS F Y", $header->udate);
            echo "<pre>".print_r($header)."</pre>";
            if (isset($header->from[0]->personal)) {
                $personal = $header->from[0]->personal;
        } else {
            $personal = $header->from[0]->mailbox;
        }

        $email = "$personal < ".$header->from[0]->mailbox."@".$header->from[0]->host." >";
        echo "On $prettydate, $email said \"$body\".
 <BR>";
       imap_setflag_full($imap,$i,  "\\Seen"); // Set the flag "Seen" 
    }

    imap_close($imap);

i printed the heads after i have tried to set the flag and it doesn't show up. however if i tired imap_setflag_full($imap,$i, "\\Flagged"); the flagged paraemeter would be F and if put imap_clearflag_full($imap,$i,'\\Flagged') the F would be removed... the only problem is the SEEN and UNSEEN... any help is appreciated.

thanks

Suggestions:

  1. Start using an imap function that returns an array of messages
  2. Start catching the imap returned values to see if there is an error.
  3. Try switching to UID would be my last suggestion.

I had the same problem where I was mixing Message Number with Message UID and this is why the Unseen was not (always) set. This does not seem to be your case.

$mbox = imap_open ( $account, $user, $pwd);
if( $mbox !== false ) {
  $numMsg = imap_num_msg ( $mbox );
  $msgs = imap_search($mbox, 'UNSEEN', SE_UID);

  // Go through ALL emails.
  foreach ( $msgs as $msguid ) {
    $msgno = imap_msgno ( $mbox, $msguid );
    $header = imap_headerinfo( $mbox, $msgno);
    if( $header === false ) {
      ... log error
    }
    $email = imap_body ( $mbox, $msgno );

    // Mark as Read
    $result = imap_setflag_full($mbox, $msguid, "\\Seen", ST_UID);
    if( $result === false ) {
      ... log error
    }

  }
  imap_close ( $mbox );
}