Set email address as SEEN on IMAP server

I am trying to read mail from an Imap server (Gmail). I would check if there is new mail (invisible) and check it as you can see. I wrote this code, but

imap_setflag_full 

doesn't seem to work. If I have new mail, the script cannot set the SEEN flag, and it repeats that there is always one invisible letter.

  $mbox=imap_open( "{imap.gmail.com:993/ssl/novalidate-cert}" , $this->username, $this->password); if ($mbox) { echo "Connected\n<br><br>"; } else { exit ("Can't connect: " . imap_last_error() ."\n"); echo "FAIL!\n"; }; if ($hdr = imap_check($mbox)) { $msgCount = $hdr->Nmsgs; echo "There are ".$msgCount." mail"; } else { echo "Failed to get mail"; } $result = imap_search($mbox, 'UNSEEN'); echo "<br>Result: "; print_r($result); if($result==false) echo "No email"; else{ echo "you have mail"; echo("<br>now I set the Seen flag for this mail"); rsort($result); $status = imap_setflag_full($mbox, "1", "\\Seen \\Flagged", ST_UID); } echo"<br><br>"; $result = imap_search($mbox, 'UNSEEN'); echo "<br>Result: "; print_r($result); if($result==false) echo "no mail"; else{ echo "there are still"; } 

Thank you very much.

+4
source share
2 answers

I think the problem is that you are hardcoded "1". I replaced "1" with:

 foreach ($result as $mail) { $status = imap_setflag_full($mbox, $mail, "\\Seen \\Flagged", ST_UID); } 

and it seems to work. When using ST_UID, this means actually an identifier, not a serial number.

+9
source

I don’t know how it works on the answer, and people vote for this answer. I spent all day on this answer.

Finally, I get a real solution. It works. I just install unread letters to read letters.

 <?php // Connect to gmail $imapPath = '{imap.gmail.com:993/imap/ssl}INBOX'; //$imapPath = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX'; $username = ' Your-email@gmail.com '; $password = 'Your-Password'; $email_read = 'UNSEEN'; // try to connect $inbox = imap_open($imapPath,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error()); $emails = imap_search($inbox,$email_read); $ids = array(); foreach($emails as $key => $mail) { $ids [] = $mail; // Do here whatever you want. } // Setting flag from un-seen email to seen on emails ID. imap_setflag_full($inbox,implode(",", $ids), "\\Seen \\Flagged"); //IMPORTANT // colse the connection imap_expunge($inbox); imap_close($inbox); ?> 
+3
source

All Articles