Imap remains open after you search and doesn't close until you call imap_close. Firstly the code shouldn't be written like that, while should not operate on a fail condition of a function exec, that's just bad coding practice and that's what causes a loop here. If there are no messages in the email box, imap search will return false, and you will have a loop until it gets a message... Here is what your scrip does: First run: Get all messages Gets array of messages Fails while condition Fetch Print Delete all Exit Next time: Get messages Gets false (no messages) Hits while loop where by it will continue to send imap requests until a message hits the mail box (your almost infinite loop) ..... -- Sent from my Android phone with K-9 Mail. Please excuse my brevity. Tontonq Tontonq <rootdot@xxxxxxxxx> wrote: hi ! it works if there is / are emails in the box before script run (i use cli not web based) but after it works 1 time it doesnt work again it enters to infinite loop , at that line while(!$emails) { $emails = imap_search($inbox,'ALL'); echo "email yok\n"; print_r($emails); } imap_search($inbox,'ALL'); it doesn't try to research emails in $inbox, doesn't it stay as connected or it's just for 1 time use :S ? should i reuse imap_open everytime when i need to use imap_search ? $inbox = imap_open($hostname,$usernamex,$password) or die('Cannot connect to domain:' . imap_last_error()); function onayla() { global $inbox; $emails = imap_search($inbox,'ALL'); while(!$emails) { $emails = imap_search($inbox,'ALL'); echo "email yok\n"; print_r($emails); } echo "\nyeaah"; print_r($emails); if($emails) { rsort($emails); echo "Number of email:".imap_num_msg($inbox); foreach($emails as $email_number) { $overview = imap_fetch_overview($inbox,$email_number,0); if(stristr($overview[0]->subject,"Test")) { $message = imap_fetchbody($inbox,$email_number,1); echo "$message\n\r"; //$link=arasi('activate:','-- The',$message); //echo "\n\r".$link; #fwrite(fopen("deneme.txt",w),file_get_contents($link)); //imap_delete($inbox,'1:*'); //imap_expunge($inbox); } } } imap_delete($inbox,'1:*'); imap_expunge($inbox); }