mail loop help!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
speedamp
Forum Commoner
Posts: 45
Joined: Tue Apr 29, 2003 3:59 pm

mail loop help!

Post by speedamp »

hello everybody....

i am really close to what i want here, but can't get this script to output progress along the way.....it either times out or gives the results all at once (when it's done).....i want it to print line by line the output along the way so i can resend if it times out.

Code: Select all

while ($row = mysql_fetch_array($query)) 
{ 
$email = $row['email']; 
$fname = $row['fname']; 
$lname = $row['lname']; 
$bib = $row['bib']; 
$body = "$fname $lname,\n 
Your bib number is $bib\n"; 
stripslashes($body); 
strip_tags($body); 
stripslashes($subject); 
strip_tags($subject); 
mail($email, $subject, $body, $mailheaders); 
    echo "sending to ".$email."<Br>"; 
    flush(); 
    sleep(3); 
}
i have about 2000 entries in my db that i need to email blast....am i way off base in the way i'm doing this?

Thanks!
-Michael
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

[php_man]set_time_limit[/php_man]

and flushing in the middle of the stream is only supported on certain servers. Additionally, it's always batched.
speedamp
Forum Commoner
Posts: 45
Joined: Tue Apr 29, 2003 3:59 pm

Post by speedamp »

here is my latest idea: to write each email to a text file as they are sent....if i encounter a timeout, i should have all sent emails logged, so i can continue.

Code: Select all

ini_set('max_execution_time', '1000'); 
set_time_limit (1000); 

while ($row = mysql_fetch_array($query)) 
{ 
$email = $row['email']; 
$fname = $row['fname']; 
$lname = $row['lname']; 
$bib = $row['bib']; 
$from = "test@test.com"; 
$subject = "subject"; 
$mailheaders = "From: $from\n"; 
$mailheaders .= "Reply-To: $from\n\n"; 
$body = "$fname $lname,\n 
Your bib number is $bib\n"; 
stripslashes($body); 
strip_tags($body); 
stripslashes($subject); 
strip_tags($subject); 
stripslashes($mailheaders); 
strip_tags($mailheaders); 
mail($email, $subject, $body, $mailheaders); 
   $fp = fopen("log.txt","a"); 
   $content = "sent to ".$email."\r\n"; 
   fwrite($fp,$content); 
   fclose($fp); 
    sleep(3); 
} 
echo "done";

will this write to the file as it goes along, or will it wait (similar to my echo problems) and write it at competion?


-mike
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

have you tried it?
speedamp
Forum Commoner
Posts: 45
Joined: Tue Apr 29, 2003 3:59 pm

Post by speedamp »

yes, i have tried it on a small test area....but i do not want to test it on 1000 emails.....

does it seem like it's going to write them as they are sent, or will it write them upon completion of everything? ....meaning, if it times out, it won't write anything
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you open and close the file on each pass.. it's written as each one is done..

you are only allowing 1000 seconds to execute the script.. 1000 emails will take at least 3000, with the code you have.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

I think you can change that in the php_int, or through .htaccess...

Speedamp, be sure not to get your domain blacklisted for spamming. ;)
Post Reply