3

I am studying an application to trigger a PHP script based on new IMAP emails arriving on Gmail. What's the best way to know a new email has arrived on a Gmail IMAP account? I can't think of anything but to configure a cron job. I am running PHP + Nginx on a Linux (Ubuntu) box.

2 Answers 2

4

I found out that that's just the way celular companies developers are doing to verify their clients gmail.

Well, start making the connection normaly, then:

$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(!@imap_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

And that's it.

By the way, here's some good informations about how IMAP works. My point is that: as IMAP makes possible to mantain virtually a kind of "Live Sync" connection, if you don't want to configure a MTA to receive email (like me), so IMAP is a real option for get "email pusshed" to you.

  • The connection stays active from 5 to 10 minutes each time you connect to your email, unless you manually disconnect
  • Gmail does limit each account to 10 simultaneous connections.
  • An IMAP account, though, shoud check the mailbox and then keep an active channel to the IMAP server (IMAP-IDLE) for the preset industry standard of 29 minutes before it times out. If you set your Auto-Retrieve setting to between 20-30 minutes, that should keep your phone connected to the remote IMAP box.
  • When GMAIL gets an email it should send a response to the IMAP idle session and mobiPush should pick it up almost instantly.
  • All schedule to retrieve your Gmail mail every 10 minutes, this option will sync incoming E-mail immediately when it arrives at the Gmail servers.
2

There's only two ways to get info from an email account, connect to it, reading it regularly (for example, by a cron-job) for new messages, or have the email forwarded to your own server, which pipes new email arriving into a PHP script.

Zend_Mail, part of the Zend Framework has Zend_Mail_Storage_Imap (which can be used without the rest of the MVC structure) which can connect to Gmail to poll an account.

1
  • Thank you, Alister. As I am using Google Apps in my domain, what solution would you recomend? If cronjob, would you have an example?
    – Roger
    Feb 5, 2011 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.