2012-12-21 2 views
2

Zend_Mail_Storage_Imap 라이브러리를 사용하여 IMAP에서 전자 메일을 검색하고 있습니다.Zend를 사용하여 IMAP에서 특정 날짜까지 메일 검색

$mail = new Zend_Mail_Storage_Imap(array('connection details')); 
foreach($mail as $message) 
{ 
    if($message->date > $myDesiredDate) 
    { 
    //do stuff 
    }else{ 
    continue; 
    } 

이 코드는 가장 오래된 메일이 먼저 검색된 모든 메일을 검색합니다. 변수 $ myDesiredDate는 날짜/시간이며 그 이후의 메일은 필요하지 않습니다. 모든 메일의 검색을 건너 뛰고 각 메일의 날짜를 하나씩 확인하는 방법이 있습니까? 그렇지 않다면 맨 위에 최신 이메일을 받기 위해 $ mail 객체를 뒤집을 수 있습니까?

업데이트 : 최근 메일에서 시작하여 현재 메일의 날짜 시간을 확인하기 위해 코드를 조금 수정했습니다. 내가 이메일을 파싱하고 싶지 않은 시간이 지나면 이메일을 받게되는 순간, 나는 그 반복문을 깨뜨린다.

//time upto which I want to fetch emails (in seconds from current time) 
    $time = 3600; 
    $mail = new Zend_Mail_Storage_Imap(array('connection details')); 
    //get total number of messages 
    $total = $mail->countMessages() 

    //loop through the mails, starting from the latest mail 
    while($total>0) 
    { 
     $mailTime = strtotime(substr($mail->getMessage($total)->date,0,strlen($mail->getMessage($total)->date)-6)); 

     //check if the email was received before the time limit 
     if($mailTime < (time()-$time)) 
     break; 
     else 
     //do my thing 

     $total--; 
    } 

    //close mail connection 

$mail->close(); 

메일 개수를 0으로 시작하면 올바른 순서로 메일을 가져올 지 여부와 관련하여 내가 우려하는 유일한 점은 무엇입니까?

답변

1

내 코드가 정상적으로 작동하므로 답변 (빨리 그리고 더러운)으로 포함시켜야합니다. 이제 최신 메일에서 시작하여 현재 메일의 날짜 시간을 확인하십시오. 내가 이메일을 파싱하고 싶지 않은 시간이 지나면 이메일을 받게되는 순간, 나는 그 반복문을 깨뜨린다.

//time upto which I want to fetch emails (in seconds from current time) 
    $time = 3600; 
    $mail = new Zend_Mail_Storage_Imap(array('connection details')); 
    //get total number of messages 
    $total = $mail->countMessages() 

    //loop through the mails, starting from the latest mail 
    while($total>0) 
    { 
     $mailTime = strtotime(substr($mail->getMessage($total)->date,0,strlen($mail->getMessage($total)->date)-6)); 

     //check if the email was received before the time limit 
     if($mailTime < (time()-$time)) 
     break; 
     else 
     //do my thing 

     $total--; 
    } 

    //close mail connection 

$mail->close(); 
관련 문제