Set an email as SEEN on IMAP server
Asked Answered
R

2

7

I am trying to read mail from an Imap Server (Gmail). I would check if there are new mail (unseen) and check it as seen. I wrote this code but

imap_setflag_full

seems to not work. If I have a new mail the script is unable to put the SEEN flag and it echo me that there is always one unseen mail.

  $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 so much.

Rm answered 20/7, 2011 at 17:9 Comment(0)
C
16

I think the problem is with the "1" you have hardcoded. I replaced the "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 ID, and not a sequence number.

Carma answered 20/7, 2011 at 20:59 Comment(2)
To return UIDs instead of sequence numbers use the SE_UID option.Sheilasheilah
Just to clarify Matthew's comment: The ST_UID flag only must be passed if you search messages by internal ID, not by sequence number (default): $result = imap_search($mbox, 'UNSEEN', SE_UID); which is not being done in the question.Vacuous
A
7

I don't know how above answer is working and people are voting that answer. I've wasted my whole day on that answer.

Finally i get the real solution. This is working. I am just setting unread emails to read emails.

<?php   
// Connect to gmail
$imapPath = '{imap.gmail.com:993/imap/ssl}INBOX';
//$imapPath = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';
$username = '[email protected]';
$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);

?>
Advise answered 31/5, 2016 at 10:56 Comment(1)
In your answer, you are marking ALL unseen emails as read. But in the above answer, I can choose what email I want to mark as read inside the foreach using different conditions as necessary.Tiffanitiffanie

© 2022 - 2024 — McMap. All rights reserved.