Check Gmail IMAP via PHP for new messages in a loop -
i studying application trigger php script based on new imap emails arriving on gmail. what's best way know new email has arrived on gmail imap account? can't think of configure cron job. running php + nginx on linux (ubuntu) box.
i found out that's way celular companies developers doing verify clients gmail.
well, start making connection normaly, then:
$t1=time();//mark time in $tt=$t1+(60*1);//total time = t1 + n seconds do{ if(isset($t2)) unset($t2);//clean @ every loop cicle $t2=time();//mark time if(imap_num_msg($imap)!=0){//if there message (in inbox) $mc=imap_check($imap);//messages check //var_dump($mc); die;//vardump see data possible imap_check() , them customize }else echo 'no new messagens'; sleep(rand(7,13));//give google server breack if(!@imap_ping($imap)){//if connection not //start imap connection normal way did @ first } }while($tt>$t2);//if total time not achivied yet, beginning of loop
and that's it.
by way, here's informations how imap works. point that: imap makes possible mantain virtually kind of "live sync" connection, if don't want configure mta receive email (like me), imap real option "email pusshed" you.
- the connection stays active 5 10 minutes each time connect email, unless manually disconnect
- gmail limit each account 10 simultaneous connections.
- an imap account, though, shoud check mailbox , keep active channel imap server (imap-idle) preset industry standard of 29 minutes before times out. if set auto-retrieve setting between 20-30 minutes, should keep phone connected remote imap box.
- when gmail gets email should send response imap idle session , mobipush should pick instantly.
- all schedule retrieve gmail mail every 10 minutes, option sync incoming e-mail when arrives @ gmail servers.
Comments
Post a Comment