使用imap php函数获取未读消息的数量

时间:2013-10-25 08:39:46

标签: php mysql email zend-framework

实际上我正在使用imap功能,但问题是我得到的数量是我想要的所有电子邮件数量给我未读的邮件数量任何人都可以告诉我我该怎么做。我可以删除看到的邮件然后它工作正常,但我想保留邮件并阅读未读邮件。

我正在接受this class

的帮助
    for ($i = 0; $i < $total_messages; $i++) {

    $email = $vars->email_get($i+1);
    $ismsgdb = $vars->db_add_message($email); // vars INSERTS EACH MSG INTO DATABASE
    #Get store dir
    $dir = $vars->dir_name();
    $id_log = $vars->add_db_log($email, 'Copy e-mail - start ');

    foreach($vars->partsarray as $part){

    if($part[text][type] == 'HTML'){
        #$message_HTML = $part[text][string];
        $vars->db_update_message($part[text][string], $type= 'HTML');
    }
    elseif($part[text][type] == 'PLAIN'){
        $message_PLAIN = $part[text][string];
        $vars->db_update_message($part[text][string], $type= 'PLAIN');
    }
    elseif($part[attachment]){
    #Save files(attachments) on local disc

    foreach(array($part[attachment]) as $attach){
        $attach[filename] = $vars->mimie_text_decode($attach[filename]);
        $attach[filename] = preg_replace('/[^a-z0-9_\-\.]/i', '_', $attach[filename]);
        $vars->add_db_log($email, 'Start coping file:"'.strip_tags($attach[filename]).'"');

        $vars->save_files($vars->newid.$attach[filename], $attach[string]);
        $filename =  $dir.$vars->newid.$attach[filename];
        $vars->db_add_attach($attach[filename], $filename);
        $vars->update_db_log('<b>'.$filename.'</b>Finish coping:  "'.strip_tags($attach[filename]).'"', $vars->logid);
    }

    }elseif($part[image]){
    #Save files(attachments) on local disc

    $message_IMAGE[] = $part[image];

    foreach($message_IMAGE as $image){
        $image[filename] = $vars->mimie_text_decode($image[filename]);
        $image[filename] = preg_replace('/[^a-z0-9_\-\.]/i', '_', $image[filename]);
        $vars->add_db_log($email, 'Start coping file: "'.strip_tags($image[filename]).'"');


        $vars->save_files($vars->newid.$image[filename], $image[string]);
        $filename =  $dir.$vars->newid.$image[filename];
        $vars->db_add_attach($image[filename], $filename);
        $vars->update_db_log('<b>'.$filename.'</b>Finish  
        coping:"'.strip_tags($image[filename]).'"', $vars->logid);
        }

       }

     }
       $vars->spam_detect();
       $vars->email_setflag(); 
      //$vars->email_delete(); // WHEN I REMOVE vars THE SCRIPT WONT GRAB NEW EMAILS
      //$vars->email_expunge(); // WHEN I REMOVE vars THE SCRIPT WONT GRAB NEW EMAILS

      $vars->update_db_log('Finish coping', $id_log);
}

2 个答案:

答案 0 :(得分:1)

我没有尝试这段代码,但我认为它会毫无错误地运行。你可以尝试这样的事情:

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

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

/* grab emails */
$emails = imap_search($inbox,'UNSEEN');
 echo count($emails);

imap_close($inbox);

仅将未见参数传递给搜索功能。有关详细信息,请阅读this

答案 1 :(得分:1)

经过一天的工作,]找到了解决方案。我们需要将UNSEEM替换为UNREAD,如下所示:

$inbox = imap_open ("{".$serveur_entrant.$port.$protocole."}".$boite, $identifiant, $mot_de_passe);
$emails = imap_search($inbox,'UNREAD');
echo count($emails);
imap_close($inbox);