开发者

Using IMAP () in PHP to get recent unread emails count

I am trying to get no. of recent unread mails from a gmail account.For this I have installed IMAP in my Ubuntu system and tried some PHP iMAP functions. Here are what i have tried till now.

/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'user@gmail.开发者_运维问答com';
$password = 'user_password';

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' .    imap_last_error());

Now I am stating all my attempts. NB - I have tried each attempt by sending new mails to the testing email id

Attempt_1: Using imap_search()

$recent_emails = imap_search($inbox,'RECENT');
if ($recent_emails)
   echo count($recent_emails);
else
   echo "false return";
imap_close($inbox);

Now Output of Attempt_1 is "false return";

Attempt_2: Using imap_mailboxmsginfo()

$check = imap_mailboxmsginfo($inbox);
if ($check)
    echo "Recent: "   . $check->Recent  . "<br />\n" ;
else
    echo "imap_check() failed: " . imap_last_error() . "<br />\n";
imap_close($inbox);

Here the output is Recent:0 while I have sent 2 new mails to this id

Attempt_3: using imap_status()

$status = imap_status($inbox, $hostname, SA_ALL);
if ($status)
  echo "Recent:     " . $status->recent      . "<br />\n";
else
  echo "imap_status failed: " . imap_last_error() . "\n";

//Output Recent:0

Attempt_4: Using Using imap_search() Again with parameter NEW

$recent_emails = imap_search($inbox,'NEW');
if ($recent_emails)
   echo count($recent_emails);
else
   echo "false return";
imap_close($inbox);

Output - false return

So Where Am I WRONG? How can I get the recent unread emails count?


This function seems to work:

function CountUnreadMail($host, $login, $passwd) {
    $mbox = imap_open($host, $login, $passwd);
    $count = 0;
    if (!$mbox) {
        echo "Error";
    } else {
        $headers = imap_headers($mbox);
        foreach ($headers as $mail) {
            $flags = substr($mail, 0, 4);
            $isunr = (strpos($flags, "U") !== false);
            if ($isunr)
            $count++;
        }
    }

    imap_close($mbox);
    return $count;
}

Usage:

$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'user@gmail.com';
$password = 'user_password';

$count = CountUnreadMail($hostname, $username, $password);

I can’t claim full credit for this function. It’s a slightly edited version of sdolgy’s answer to PHP Displaying unread mail count. His version assumed POP mail. This version requires the full $hostname. I tested it with my own gmail account and it correctly reported the number of unread messages I had in my inbox.

PHP Displaying unread mail count has some pretty good reading material. Check it out.

Hope this helps.

UPDATE

From: Does Gmail support all IMAP features?

Gmail IMAP1 is a fairly complete implementation of IMAP, but the following features are currently unsupported:

\Recent flags on messages.

Verfied at: Gmail's Buggy IMAP Implementation

Gmail doesn't handle standard IMAP flags, such as "\Deleted", "\Answered", and "\Recent".

See also: Jyoti Ranjan's answer (below) for a possible solution.


I have solved it using database,This is how I have done it.

1.I made a column in users table, for ex- 'email_max_uid' INT(11) NOT NULL default 0

2.When a user loads this page first, using my sql query I retrieved the value of 'email_max_uid' of that particular users 3.connect to his gmail account automatically and got the unread mails and total mails

$inbox = imap_open('{imap.gmail.com:993/imap/ssl}INBOX','user's email id','user's password') or die('Cannot connect to Gmail: ' . imap_last_error());
        $unread_emails = imap_search($inbox,'UNSEEN');
        $emails = imap_search($inbox,'ALL', SE_UID);

4.Then

if ('email_max_uid') = 0
        $recent_mails_count = count($unread_emails);
    else
        $recent_mails_count = max($emails) - Array['email_max_uid'];

5.Show the recent mails count

echo count($recent_mails_count);

6.I have put a link which makes the user log in to his gmail account without asking his email id and password When the users log in to his account, email_max_uid is updated with count($emails)

  1. Now again when the users visits that page, it follows the above proceedure and his no. of recent emails will be displayed on the page.

*My solution is for the case where a user can log in to his gmail account only through this application, if he logs into his account from outside, the database wont be updated so that wrong no. of mail counts will be shown.In my problem case, user even doesn't know his gmail id and password provided by the company to see only their mails. So he cant log in to his this account from outside. That's why this solution works.

This is how I solved it 3 days ago.Sorry for late reply

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜