Search code examples
phpimap

Using IMAP to fetch emails in php


I am using Imap function to fetch the emails through yahoo mail. Then i set the flag on each email because when in future i call the imap search i will not get the emails that are already fetched.This is how i get the emails.

$emails = imap_search($inbox,"UNFLAGGED");

Then i apply the flag using the code :

foreach($emails as $email_number) {
       imap_setflag_full($inbox, $email_number, "\\Flagged \\Seen", ST_UID);

But when i call the imap_search($inbox,"UNFLAGGED"); i get the same emails. The emails which are already been flagged should not fetch when i call the imap_search second time.

I Also tried imap_search($inbox,"unflagged") and imap_search($inbox,"Unflagged") and imap_search($inbox,"UnFlagged") but still all emails are fetched.

I tried imap_search($inbox,"UNSEEN") it is useful in case when the email is not read manually by user (Logging in yahoo mail and then read). In my case i need all the emails which are not "Flagged" by me using Imap.

Need help on this. Thanks in advance


Solution

  • You are searching message sequence numbers, but setting flags on UIDs. Pick one or the other. I suggest always using UIDs. Have you tried adding SE_UID to search:

    $emails = imap_search($inbox, "UNFLAGGED", SE_UID);