Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check Gmail IMAP via PHP for new messages in a loop

Tags:

php

gmail

imap

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.

like image 599
Roger Avatar asked Dec 10 '25 03:12

Roger


2 Answers

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.
like image 55
Roger Avatar answered Dec 12 '25 17:12

Roger


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.

like image 43
Alister Bulman Avatar answered Dec 12 '25 19:12

Alister Bulman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!