1

I am running this script it is fine for one context but when I try this on 4 recipients it is not working just showing this error:

Fatal error: Maximum execution time of 30 seconds exceeded in D:\Hosting\8011955\html\admin\newsletter.php on line 60

How can I improve this code? I just want to send email not more than 200.

here is the code:

if(!(is_array($errors)))
{
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: \"".$from_name."\" <".$from_email.">\n";

    $query="SELECT email From newsletter WHERE visible='1'";
    $result=mysql_query($query) or mysql_error();
    while($rowdata=mysql_fetch_array($result))
    {
       $headers .= "To: \"".$to_name."\" <".$rowdata['email'].">\n";    
           mail($rowdata['email'], "$sub",$message, $headers);
    }   
}
2
  • And what do the logs say, mmm? Commented Nov 1, 2011 at 19:21
  • 2
    mail() is a slow function. you won't be able to speed it up without dumping it and switching to something better, like PHPMailer or Swiftmailer Commented Nov 1, 2011 at 19:23

3 Answers 3

2

How many matching rows are in the newsletter table?

As an aside, there's a logical error in your code -- it will add a new To: field each time through the loop, so the first mail will go to:

To: [email protected]

And the second one will go to:

To: [email protected]
To: [email protected]

This will result in [email protected] receiving one message for every person on your mailing list, [email protected] receiving one less message, and so on. Don't do this. :)

Sign up to request clarification or add additional context in comments.

2 Comments

BE CAREFUL! If you do not flush out the "to" during every iteration you may end up sending the email to un-intended recipients. There was a known issue with this in the old FMail library.
ohhh thanks...i got you... will you suggest how can i improve the code. so that i can send the email to 100 contacts...
1

By default, PHP scripts run for 30 seconds and then stop. This value is controlled by the max_execution_time value in php.ini. You can override this value within the current script by calling the set_time_limit() function. Each time you call set_time_limit() the script execution timer starts over at zero. So you can put it inside of your while {} loop to give your script as much time as it needs.

while($rowdata=mysql_fetch_array($result))
{
   set_time_limit(30); // Reset script timer and give script 30 seconds to send the next email
   $headers .= "To: \"".$to_name."\" <".$rowdata['email'].">\n";    
   mail($rowdata['email'], "$sub",$message, $headers);
}

Comments

1

put below line at the beginning of your function.

set_time_limit(0);

this helps to run a script that needs to execute for unknown time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.