Jump to content

Large IMAP loop starts to hang around 20,000 emails...


youneek

Recommended Posts

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);
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

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 = '';
    }
}

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.