2017-02-16 98 views
0

我的工作vtigerCRM的模塊上使用PHP的IMAP與不同的應用條件,從郵件服務器獲取電子郵件那些不存儲在我們的數據庫。如何獲取電子郵件使用PHP IMAP

$hostname = '{imap.gmail.com:993/ssl/novalidate-cert}'; 
$username = '[email protected]'; 
$password = 'mypswd'; 

$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error()); 
$emails = imap_search($inbox,'FROM [email protected]'); 

if($emails) { 

    $output = ''; 

    rsort($emails); 

    foreach($emails as $email_number) { 

     $overview = imap_fetch_overview($inbox,$email_number,0); 
     $message = imap_fetchbody($inbox,$email_number,2); 
     $message = trim(quoted_printable_decode($message)); 
     $output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">'; 
     $output.= '<span class="subject">'.$overview[0]->subject.'</span> '; 
     $output.= '<span class="from">'.$overview[0]->from.'</span>'; 
     $output.= '<span class="date">on '.$overview[0]->date.'</span>'; 
     $output.= '</div>'; 
     $output.= '<div class="body">'.$message.'</div>'; 
     echo $output;die; 
    } 

    echo $output; 
} 

這個完整的代碼工作正常,但希望只獲取新郵件那些不被存入我的數據庫還,所以,一旦郵件被保存在我的數據庫不應該再次存儲。

+0

你可以在日期 – nogad

+0

過濾或從數組中獲得電子郵件並比較你的imap電子郵件數組(差異) –

+0

,但在這種情況下,我只能一天取一次,但我想在每個3後運行cron作業分鐘,以便它會不斷更新我的記錄。 –

回答

0

問題解決了,我使用日期參數,使其取最小值郵件過濾,我存儲在MESSAGE_ID到我的數據庫,並使用該MESSAGE_ID我提出一個條件,如果它已經存在,或者沒有在我的數據庫。

相關問題