2011-02-05 116 views
3

我正在研究基於到達Gmail的新IMAP電子郵件來觸發PHP腳本的應用程序。知道新電子郵件的最佳方式是通過Gmail IMAP帳戶獲得的?我想不出什麼,但配置一個cron作業。我在Linux(Ubuntu)盒子上運行PHP + Nginx。通過PHP檢查循環中的新郵件的Gmail IMAP

回答

4

我發現這只是celular公司開發人員在做驗證客戶端gmail的方式。

好,開始建立連接normaly,則:

$t1=time();//mark time in 
$tt=$t1+(60*1);//total time = t1 + n seconds 

do{ 
    if(isset($t2)) unset($t2);//clean it at every loop cicle 
    $t2=time();//mark time 
    if(imap_num_msg($imap)!=0){//if there is any message (in the inbox) 

     $mc=imap_check($imap);//messages check 
     //var_dump($mc); die;//vardump it to see all the data it is possible to get with imap_check() and them customize it for yourself 


    }else echo 'No new messagens'; 

    sleep(rand(7,13));//Give Google server a breack 
    if([email protected]_ping($imap)){//if the connection is not up 
     //start the imap connection the normal way like you did at first 
    } 

}while($tt>$t2);//if the total time was not achivied yet, get back to the beginning of the loop 

就是這樣。

順便說一下,這裏有關於IMAP如何工作的一些很好的信息。我的觀點是:因爲IMAP使得實際上可以實現一種「實時同步」連接,所以如果你不想配置一個MTA來接收電子郵件(像我一樣),所以IMAP是一個真正的選擇,可以讓「電子郵件欺騙「 給你。

  • 的連接保持每個連接到您的電子郵件時間5〜10分鐘的活性,除非你手動斷開
  • 的Gmail不限制每個賬戶10個同時連接。
  • 但是,IMAP帳戶應該檢查郵箱,然後在超時之前保留29分鐘的預設行業標準的IMAP服務器(IMAP-IDLE)的活動頻道。如果您將自動恢復設置設置爲20-30分鐘,則應使手機連接到遠程IMAP盒。
  • 當GMAIL收到一封電子郵件時,它應該向IMAP空閒會話發送一個響應,mobiPush應該幾乎立即收到它。
  • 所有時間表每10分鐘檢索一次Gmail郵件,該選項會在到達Gmail服務器時立即同步傳入的電子郵件。
1

只有兩種方式才能從電子郵件帳戶獲取信息,連接到它,定期閱讀新郵件(例如,通過cron-job),或者將電子郵件轉發到您自己的服務器,電子郵件到達一個PHP腳本。

Zend_Mail,Zend Framework的一部分具有Zend_Mail_Storage_Imap(可以在沒有其他MVC結構的情況下使用),它可以連接到Gmail來輪詢帳戶。

+0

謝謝,阿利斯特。當我在我的域中使用Google Apps時,您會推薦什麼解決方案?如果cronjob,你會有一個例子嗎? – Roger 2011-02-05 17:59:47