2010-02-01 3 views
8

Ho uno script PHP che recupera i messaggi da una casella di posta. Io uso la funzione imap_search: $emails = imap_search($mbox, 'UNSEEN');imap_search limita il numero di messaggi restituiti

C'è un modo per limitare il numero di messaggi restituiti. In questo momento su enormi cassette postali ricevo come 5000 messaggi. Voglio solo i primi 20 ordinati per data.

C'è un modo per farlo?

Grazie.

risposta

6

La funzione imap_search ha un attributo CRITERI è possibile utilizzare per limitare i messaggi in diversi modi:

ALL - restituire tutti i messaggi che corrispondono al resto dei criteri
risposto - abbinare i messaggi con il flag \ RISPOSTO set
BCC "stringa" - abbinare messaggi con "stringa" nel campo Ccn:
pRIMA "data" - abbinare messaggi con data: prima di "data"
CORPO "stringa" - abbinare messaggi con "stringa" nel corpo del messaggio
CC "stringa" - abbinare i messaggi con "stringa" in Cc: campo
CANCELLATO - Partita di messaggi eliminati
contrassegnati - abbinare i messaggi con il \ contrassegnato (a volte indicato come importante o urgente) bandiera impostato
DA "stringa" - Partita di messaggi con "stringa" nel campo Da:
pAROLA cHIAVE "stringa" - messaggi partita con "stringa" come parola chiave
nUOVO - partita di nuovi messaggi
vECCHIO - abbinare i vecchi messaggi
ON "data" - abbinare messaggi con data: "data" matching
RECENTE - corrispondenza dei messaggi con il set di flag \ RECENT
SEEN - messaggi di corrispondenza tha t sono stati letti (è impostato il flag \ VISTO)
Dal momento che "date" - corrisponde messaggi con Data: dopo "Data" "stringa"
SOGGETTO - abbinare messaggi con "stringa" in Oggetto:
TESTO "stringa "- abbinare messaggi con testo 'stringa'
TO 'stringa' - partita di messaggi con 'stringa' nel campo a: senza risposta - abbinare i messaggi che non hanno ricevuto risposta
eliminate - messaggi partita che non vengono eliminati
senza bandiera - corrispondenza messaggi non contrassegnati
UNKEYWORD "stringa" - corrisponde ai messaggi che non hanno la parola chiave "stringa"
INEDITE - messaggi partita che non sono stati ancora letto

+2

Questo restringerà la ricerca, ma limiterà la quantità di posta da restituire? –

+0

@Anthony: questo è l'unico modo per restringere la roba un pò in nessun altro modo per quanto ne so. Ce n'è uno? – Sarfraz

+0

Per restringerlo, è l'unico modo. Ma il suo criterio è un numero limite per, quello che sto assumendo, i messaggi * all *, che 'imap_search' non fornisce. Potrebbe esserci un trucco, vedere la mia modifica e fammi sapere cosa ne pensi. –

1

imap_sort vi permetterà sia per ordinare e filtrare al tempo stesso

Ma ancora, non permetteranno di limitare al 'top 20' a destra al la chiamata di funzione.

0

Per risolvere questo problema in modo da:

1.You potrebbe limitare il no di risultato restituito riducendo il no di dati utilizzando il dato criteri 2. prelevare qualche ultimo restituiti messaggi di posta.g 15

$this->msgCounts = imap_search($imap_resource, 'SUBJECT "hello dolly" SINCE "8 April 2003"', SE_UID); 

E poi ecco un esempio per prelevare gli ultimi 15 restituiti e poi passare avanti e indietro per vedere più risultati o older.Note questo presuppone che dispone di un pulsante in avanti e più che impostare $ _GET variabili.

$this->msgCounts = $messageCounts; 
     $multiarray=[]; 
     \Session::put('totalmsg',$this->msgCounts);    //Sav etotal no of message in folder to session to determine if to allow next or previous 

     if($this->msgCounts > 15)        //MESSAGES IS MORE THAN WE NEED GET 20 
     { 
      $offcut = 15;          //default offcut 

      /** 
      * Viewing previous or next messages 
      **/ 
       if(isset($_GET['msgs']) && $_GET['msgs'] == 'older') 
       { 
       $this->msgCounts = \Cache::has('msgpointer') ? \Cache::get('msgpointer') : $this->msgCounts; 
        $msgOffset = $this->msgCounts - $offcut; //get +15 messages 

        if($offcut > $msgOffset) { 
         $msgOffset = $msgOffset + 5;   //if less than 15 get 10 
         $offcut = 10; 
        } 
        if($offcut > $msgOffset) { 
         $msgOffset = $msgOffset + 5;   //if less than 10 get 5 
         $offcut = 5; 
        } 
        if($offcut > $msgOffset) { 
         $msgOffset = $msgOffset + 3;   //if less than 3 get 2 
         $offcut = 2; 
        } 
        if($offcut > $msgOffset) { 
         $msgOffset = $msgOffset + 2;   //if less than 2 get 1 
         $offcut = 1; 
        } 


        \Cache::put('msgpointer',$msgOffset,60 * 60 * 24); 
       } 

       if(isset($_GET['msgs']) && $_GET['msgs'] == 'newest') 
       { 
        $this->msgCounts = \Cache::has('msgpointer') ? \Cache::get('msgpointer') : $this->msgCounts; 
        $msgOffset = $this->msgCounts + $offcut; //get +15 messages 

        if($msgOffset > $messageCounts) { 
         $msgOffset = $msgOffset - 5;   //if not up to 15 get 10 
         $offcut = 10; 
        } 
        if($msgOffset > $messageCounts) { 
         $msgOffset = $msgOffset - 5;   //if not up to 10 get 5 
         $offcut = 5; 
        } 
        if($msgOffset > $messageCounts) { 
         $msgOffset = $msgOffset - 3;   //if not up to 5 get 2 
         $offcut = 2; 
        } 
        if($msgOffset > $messageCounts) { 
         $msgOffset = $msgOffset - 2;   //if not up to 2 get 1 
         $offcut = 1; 
        } 


        \Cache::put('msgpointer',$msgOffset,60 * 60 * 24); 
       } 


      // LOOP THROUGH LAST 20 MESSAGES IF THERE MORE THAN 10 MESSAGES 
      for ($i = $this->msgCounts; $i > $this->msgCounts - $offcut; $i--) 
      { 

       $header = imap_header($this->conn,$i);        //GET HEADER INFO USING IMAP FUNCTION 
       $uid = imap_uid($this->conn,$i);        //GET UNIQUE MESSAGE ID FOR READING MESSAGE LATER 


       //SAVE ALL MESSAGE INFO IN ARRAY 
       $tobox = $header->reply_to[0]->mailbox ? $header->reply_to[0]->mailbox : 'noreply'; 
       $tohost = $header->reply_to[0]->mailbox ? $header->reply_to[0]->host : 'email.com'; 
       $toaddress = $tobox.'@'.$tohost; 


       $mailbox = isset($header->from[0]->mailbox) ? $header->from[0]->mailbox : 'no-reply'; 
       $host = isset($header->from[0]->host) ? $header->from[0]->host : 'email.com'; 
       $fromaddress = $mailbox.'@'.$host; 

       $array = ['toaddress' => isset($header->toaddress) ? $header->toaddress : isset($header->to) ? $header->to[0]->mailbox.'@'.$header->to[0]->host : $toaddress,'date' => isset($header->date) ? $header->date : date('Y-m-d'),'subject' => isset($header->subject) ? $header->subject : "no subject" ,'from' => isset($header->from[0]->personal) ? $header->from[0]->personal :$fromaddress,'unseen' => isset($header->Unseen) ? $header->Unseen : 'S', 'uid' => isset($uid) ? $uid : $i,'fromemail' => $fromaddress]; 
       //PASS A MESSAGE INFO INTO A MULTI ARRAY 
       $multiarray[] = $array; 

      } 

È possibile impostare la data in cui è ottenere da essere 90 giorni prima di allora se un lot.return esso blocco per blocco, come scuse above.My per l'utilizzo di alcune classi helper laravel lì, tutto è ben commentata. Spero che questo aiuti qualcuno!