I am using below code to read my gmail inbox and it's working fine for reading.
But the issue is some of the mail are not set as seen so they comes every time.
As per my observation mails which do not change there flag from unseen to seen are the mails which contains some HTML in it.
Below is my code:
public function getMessages($type = 'text') {
$stream = $this->imapStream;
$emails = imap_search($stream, 'UNSEEN');
$messages = array();
if ($emails) {
$this->emails = $emails;
$i = 0;
foreach ($emails as $email_number) {
$this->attachments = array();
$uid = imap_uid($stream, $email_number);
$messages[] = $this->loadMessage($uid, $type);
if ($i == $this->limit) {
break;
}
$i++;
echo "seen status=>".imap_setflag_full($stream, $email_number, "\\Seen", ST_UID);
//echo "seen status=>".imap_clearflag_full($stream, $email_number, "\\Seen");
}
}
return $messages;
}
This is the line i am using to manually change status
echo "seen status=>".imap_setflag_full($stream, $email_number, "\\Seen", ST_UID);
seen status always return 1 as result but in inbox it shows as unread.
You are using Message Sequence Numbers (MSNs), but providing the ST_UID
flag to your flag functions, changing them to use UIDs. Most MSNs will not generally be valid UIDs.
Either use UIDs everywhere (with FT_UID
and ST_UID
and the like to all functions) or MSNs everywhere (never use UID flags, and don't expunge anything while looping.)
imap_setflag_full($stream, $email_number, "\\Seen");
or
imap_setflag_full($stream, $uid, "\\Seen", ST_UID);
If you use the UID version of search, you don't need to call imap_uid
.