youneek Posted April 1, 2009 Share Posted April 1, 2009 Does anyone know why this starts to go so slow or a way to get this script working better? I have tested retrieving the higher number messages and can retrieve them fine individually... <? set_time_limit(0); $imap = imap_open("{imap.server.loc}", "emailaddy", "pass")or die(imap_last_error()."<br>Connection Failure!"); $count = imap_num_msg($imap); $fh = fopen('results.csv', 'w') or die("can't open file"); for($i=1; $i<=$count; $i++) { $header = imap_header($imap, $i); $prettydate = date("m/d/Y", $header->udate); $user = strtolower($header->sender[0]->mailbox); $host = strtolower($header->sender[0]->host); if(!empty($user)){ fwrite($fh, $user.'@'.$host.','.$prettydate."\n"); } } imap_close($imap); ?> Quote Link to comment https://forums.phpfreaks.com/topic/152117-large-imap-loop-starts-to-hang-around-20000-emails/ Share on other sites More sharing options...
youneek Posted April 3, 2009 Author Share Posted April 3, 2009 I have tested the tool. If I stop and reset it to start at 20,000 it runs at full speed again... Any suggestions? for($i=20000; $i<=$count; $i++) { Quote Link to comment https://forums.phpfreaks.com/topic/152117-large-imap-loop-starts-to-hang-around-20000-emails/#findComment-799962 Share on other sites More sharing options...
JeanieTallis Posted April 3, 2009 Share Posted April 3, 2009 Maybe, its the power of the code. The weaker the code, and yet the more emails it sends out, the slower it'll take as its dealing with more loops. I'm not sure. This isn't what I study at. Quote Link to comment https://forums.phpfreaks.com/topic/152117-large-imap-loop-starts-to-hang-around-20000-emails/#findComment-799971 Share on other sites More sharing options...
redarrow Posted April 3, 2009 Share Posted April 3, 2009 you can not use php to send as many emails as you want, you have to use a email sending program. php was not design to send multiple emails like a huge mailing list. You can use the sleep() function, but like i say php is not used for a mailing list. your php sever will eventually blow up. Quote Link to comment https://forums.phpfreaks.com/topic/152117-large-imap-loop-starts-to-hang-around-20000-emails/#findComment-799982 Share on other sites More sharing options...
.josh Posted April 3, 2009 Share Posted April 3, 2009 your php sever will eventually blow up. holy crap! I bet that sure would piss the guys off at the data center. Would it make other servers connected to it blow up too?? Quote Link to comment https://forums.phpfreaks.com/topic/152117-large-imap-loop-starts-to-hang-around-20000-emails/#findComment-800053 Share on other sites More sharing options...
monokrome Posted April 3, 2009 Share Posted April 3, 2009 PHP doesn't have a garbage collector... With that said, my guess is that you're using way too much memory maybe. Unset doesn't free memory so that wont help, and you're using (40,000*SizeOfUserVariables)kb by the end of the script. I would probably not use PHP to send 40,000 emails in a single refresh, regardless. Quote Link to comment https://forums.phpfreaks.com/topic/152117-large-imap-loop-starts-to-hang-around-20000-emails/#findComment-800065 Share on other sites More sharing options...
MadTechie Posted April 3, 2009 Share Posted April 3, 2009 PHP doesn't have a garbage collector... Erm.. Yes it does! PHP does garbage collection at 3 points 1. When you use unset() or other cleanup options! 2. When you leave a function (php clears any variable that leaves scope) 3. When the script ends (php clears any variable that leaves scope) Quote Link to comment https://forums.phpfreaks.com/topic/152117-large-imap-loop-starts-to-hang-around-20000-emails/#findComment-800151 Share on other sites More sharing options...
corbin Posted April 4, 2009 Share Posted April 4, 2009 PHP doesn't have a garbage collector... With that said, my guess is that you're using way too much memory maybe. Unset doesn't free memory so that wont help, and you're using (40,000*SizeOfUserVariables)kb by the end of the script. I would probably not use PHP to send 40,000 emails in a single refresh, regardless. Besides MadTechie's points, the OP is also resetting variables. Your point would only be valid if he were making a new memory space for each thing. (I.E. if he were cramming it all into an array or something.) @OP: I would say that the problem is either the imap_header() call or the 20,000 fwrite calls ;p. Checking if it's the imap_header() call could be kind of difficult, so the easiest way would be to check if it's the fwrite call ;p. You could try something like: $content = ''; $f = fopen(...); for($i = 0; $i < 20000; ++$i) { $content .= ...; if(strlen($content) >= 8192) { //content is 8KB fwrite($f, $content); $content = ''; } } Quote Link to comment https://forums.phpfreaks.com/topic/152117-large-imap-loop-starts-to-hang-around-20000-emails/#findComment-800867 Share on other sites More sharing options...
youneek Posted April 6, 2009 Author Share Posted April 6, 2009 It's not sending emails and is only retrieving the headers to write to a file. I run it locally and can see it's using hardly any ram... I have another script that can write 40,000 lines using fwrite without any issues. Is there a more effective way to do what I am doing? Quote Link to comment https://forums.phpfreaks.com/topic/152117-large-imap-loop-starts-to-hang-around-20000-emails/#findComment-802832 Share on other sites More sharing options...
corbin Posted April 6, 2009 Share Posted April 6, 2009 Well, if it's not the file stuff, then it's most likely $header = imap_header($imap, $i). I wonder if there is an alternative IMAP library.... Just of curiosity, try running a simple benchmark in which you call $header = imap_header($imap, $i); and do nothing with it. Try doing it with 20,000+ iterations, and 5,000 iterations and see if there is a difference in requests/second. Quote Link to comment https://forums.phpfreaks.com/topic/152117-large-imap-loop-starts-to-hang-around-20000-emails/#findComment-802869 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.