Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mass email problem

Status
Not open for further replies.

karren

Programmer
Feb 26, 2002
42
CA
hello everyone,

i have created a mailing list tool that sends out mass emails to email addresses that are pulled out from a mysql database.

the problem is that there are over 1000 email addresses that it loops through and the script seems to time out after sending to the first 200 users or so.

is there a solution for sending out mass emails? this database of emails could potentially grow to be over 10,000.

here's the code i have so far:

<?php
$subject = $HTTP_POST_VARS["subject"];
$subject = stripslashes($subject);
$content_1 = $HTTP_POST_VARS["content_1"];
$content_1 = htmlentities($content_1, ENT_QUOTES);
$content_1 = stripslashes($content_1);
$content_1 = "<font face=\"arial\"> ". $content_1 ." </font>";

$SQL = "SELECT EmailAddress FROM $db";
if(!$result = mysql_query($SQL)) die("Query died for $SQL");

while($row = mysql_fetch_array($result))
{
$EmailAddress2[] = $row["EmailAddress"];
}
$subject2 = $subject;
$headers = "Content-Type: text/html; charset=iso-8859-1\n";
$headers .="From: name<name@name.com>";
$mailbody = stripslashes($content_1);

$max= count($EmailAddress2);

for ($i=0; $i < ($max); $i++)
{
if(!mail($EmailAddress2[$i], $subject2, $mailbody, $headers)) die ('Email not sent');
}

?>

any help would be greatly appreciated!!

thanks in advance :)

karren
 
The problem is that a PHP script running through a web server has a maximum time it is allowed to run. This is set in php.ini by max_execution_time. You could change that value.

I've done this kind of thing before, any my solution (on Linux) was to have two scripts. One script interacts with the user to determine to which addresses the message should be sent. It then records the necessary addresses in a MySQL database.

Then a second script, invoked by cron, would process the list of addresses. Since the second script is running independent of a web server, it isn't constrained by max_execution time.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Also, it would be less strain on your email server if you put 30-50 email addresses in a single email and let the MTA handle the delivery. Put the addresses in a BCC if you don't broadcast the list to everyone receiving the email.
 
thanks for the very fast responses!!

i understand a bit better how it all works.

do you know whether there is some type of php function that could pause the execution time of my script and then continue on after a few seconds?

i was reading something about a function called sleep(), but i couldn't find out more about it. php.net's function search seems to be down at the moment.

thanks again :) i really appreciate it.

karren
 
sleep(seconds);

is the command.

Bastien

Cat, the other other white meat
 
I guess it would also be possible to do a header() to the same page, with some variable for progression..

eg:
check how many rows and get first 30.
mail first 30, then move to the same page, with ?prog=30
then get rows from 31 to 61 and move to ?prog=61

etc etc

just a dirty fix if the other way does not work.

Olav Alexander Mjelde
Admin & Webmaster
 
thanks for all your responses, they're so helpful. i'm gonna test out the function tonight and post the code on here if it works!

thanks again,

karren

 
i got it to work with the following code:



<?php
$subject = $HTTP_POST_VARS["subject"];
$subject = stripslashes($subject);
$content_1 = $HTTP_POST_VARS["content_1"];
$content_1 = htmlentities($content_1, ENT_QUOTES);
$content_1 = stripslashes($content_1);
$content_1 = "<font face=\"arial\"> ". $content_1 ." </font>";

//query email addresses from db

$SQL = "SELECT EmailAddress FROM $db";
if(!$result = mysql_query($SQL)) die("Query died for $SQL");

while($row = mysql_fetch_array($result))
{

//collect emails in array

$EmailAddress2[] = $row["EmailAddress"];
}
?>

<div align="center">
<div
align=left
style='
display: block;
color: #000000;
background-color: #cccccc;
border: 1;
width: 250;
height : 300px;
overflow: auto;
'>


<?php
$subject2 = $subject;
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: me<info@me.com>\r\n";

$mailbody = stripslashes($content_1);

$max= count($EmailAddress2);

//the number of email address to hold per session
$hold=0;
$letters_sent=0;
$notsent=0;
set_time_limit(0);
for ($i=0; $i < ($max); $i++)
{

if(mail($EmailAddress2[$i], $subject2, $mailbody, $headers))
{

$hold++;
$letters_sent++;
echo($EmailAddress2[$i]." <b>sent!</b><br />");
//there should be a 3 second delay between emails. maybe this is excessive but it work better for me that way
sleep(3);
if($hold==100) //if 100 emails have been sent then code should rest for 2 minutes then execute after 2 mins
{
sleep(120);
$hold=0;
}
}
else
{
$notsent++;
echo($EmailAddress2[$i]." <font color=\"#ff0000\"><b>not sent</b></font><br />");
}

}
$sentstatus="<p>Newsletter sent!</p>";
?>
</div>
</div>
<p><b><?=$letters_sent?></b> newsletters sent<br /><b><?=$notsent?></b> newsletters failed</p>


thank all!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top