View Full Version : Slooooow script processing, any help appreciated

11-04-2006, 12:10 AM
Hi all

I'm a total PHP newbie so please bear with me.

I have a form that posts data into a mysql db then mails the form data to several email addresses. The email addresses are drawn from a table in the mysql db. This all works, but processing the form takes about a minute or more. User clicks 'submit' and then it takes forever to process.

This is with only 3 email addresses to send to, and I'm intending to have maybe a hundred or so. I'm thinking that the sending of the emails is holding it up - ie the script won't continue to the next instruction until the mail is sent? This is a guess, but I can't see why else it would take so long to process.

I have a good server machine - Intel Pentium D, 1gb ram, SATA drives - the machine is fine.
OS is Fedora Core 5, 64 bit.
PHP 5.1.4
apache 2.2

I'll insert the code here - I know it's a bit messy, like I say, it's my first effort

// Grab our config settings

// Grab the FreakMailer class

$mail = new PHPMailer();

/* grabs the POST variables and puts them into variables that we can use */

//////////////////////////////////////// CONNECT TO MYSQL DB ////////////////////
$connection=mysql_connect("host","db", "***") or die("Unable to connect!");

mysql_select_db("db") or die("Unable to select database!");

$query = ("SELECT a_Name,a_Surname,a_email FROM agents_pn");
$result1 = @MYSQL_QUERY($query);

while ($row = mysql_fetch_array ($result1))

// HTML body
$body = "Hello " . $row["a_Name"] . "</font>, <p>";
$body .= "This is an automatically generated email, if you receive this in error, please notify ********, on 0800 ******<p>";
$body .= "We have the following ******** for you <br>"
."First Name: ".$firstName."<br>"
."Last Name: ".$lastName."<br>"
."E-mail: ".$email."<br>"
."Phone: ".$phone."<br>"
."Fax: ".$fax."<br>"
."Mobile: ".$mobile."<br><br>--------ADDRESS--------<br>"
."Street Address: ".$address."<br>"
."City: ".$city."<br>"
."Post Code: ".$zipCode."<br>"
."Looking For: ".$inquiring."<br>"
."With a minimum of ".$beds." beds<br>"
."in: ".$area."<br>"
."With a maximum price of: ".$maxprice."<br>"
."For their wishlist, they typed the following: ".$other."<br>"
."Where did you hear about us? ".$heardAbout."<br>"
$body .= "*******! <br>";
$body .= "Sincerely, <br>";
$body .= "********";

$mail->From = "***********";
$mail->FromName = "***********";
$mail->Body = $body;
$mail->AltBody = $text_body;
echo "There has been a mail error sending to " . $row["a_email"] . "<br>";
echo "Mail sent to: " . $row["a_email"] . "<br>";
// Clear all addresses and attachments for next loop


echo "Thank you for using *****************.";
echo "<br>";
echo "<br>";
echo "**********.<br>";

// -redundant $mailContent="--------CONTACT--------\n"
// ."First Name: ".$firstName."\n"
// ."Last Name: ".$lastName."\n"
// ."E-mail: ".$email."\n"
// ."\n\n--------PHONE--------\n"
// ."Phone: ".$phone."\n"
// ."Fax: ".$fax."\n"
// ."Mobile: ".$mobile."\n\n--------ADDRESS--------\n"
// ."Street Address: ".$address."\n"
// ."City: ".$city."\n"
// ."Post Code: ".$zipCode."\n\n--------INFO--------\n"
// ."Looking For: ".$inquiring."\n"
// ."With a minimum of ".$beds." beds\n"
// ."in: ".$area."\n"
//."with the wishlist: ".$other."\n"
// ."Where did you hear about *********? ".$heardAbout."\n";
//commented out $toAddress=($row["a_email"]); /* ALL AGENTS PN! */
$subject="A NEW ***************";
$recipientSubject="Your ********* details";
$receiptMessage = "Thank you ".$firstName." for using ********!\n\n\nHere is what you submitted to us:\n\n"
."First Name: ".$firstName."\n"
."Last Name: ".$lastName."\n"
."E-mail: ".$email."\n"
."Phone: ".$phone."\n"
."Fax: ".$fax."\n"
."Mobile: ".$mobile."\n\n--------ADDRESS--------\n"
."Street Address: ".$address."\n"
."City: ".$city."\n"
."Post Code: ".$zipCode."\n\n--------INFO--------\n"
."Looking For: ".$inquiring."\n"
."With a minimum of ".$beds." beds\n"
."and a maximum price of ".$maxprice."\n"
."in: ".$area."\n"
."with the wishlist: ".$other."\n"
."Where did you hear about us? ".$heardAbout."\n";
mail($email, $recipientSubject, $receiptMessage,"From:$FromName");

$query="INSERT INTO hh1 (
$result=mysql_query($query) or die("Error in query:".mysql_error());
//if ($result)
// echo mysql_affected_rows()." row inserted into the database effectively.";




11-04-2006, 04:25 PM
Nothing really jumps out. I can't say for certain, there could be a number of reasons:

1) PHP's string handling is slow. It could be your loop to generate the email body, but it doesn't seem TOO bad

2) Your database could be misconfigured and your queries are taking a long time, or the fetch rows are taking a long time.

3) Your mail server could be misconfigured and taking forever to handle your sends.

Etc, etc.

The best thing for you to do is to take this opportunity to learn how to time your scripts. One way is to copy your script and replace the post variables with hardcoded default values, and run the script from the command line:

time php myscript.php

you'll see how long it takes. then, comment out sections of code to see how the time is effected.

Another way is to use strace to watch your code execute, but if you're not familiar with the output, it could be confusing.

There are also code profilers out there to help you.

But, this is a perfect time to learn how to get this kind of information for yourself. Try to figure it out, and if you need more guidance, let us know.

11-05-2006, 10:01 PM
Thanks for your reply.

I used the microtime function to time sections of the code to see where the holdup was.

The mysql stuff is processing in - for example - 0.00038 seconds, so no problem there

the mail send processes however take 21 seconds, 16 seconds, varying amounts of ridiculously long times.

I'm going to have a good long look at the mail server, and perhaps send the mails to my iSP be sent rather than internally - but as a quick fix, is it possible to give a response back to the user before the mail process is finished?
I don't actually care how long it takes to send the mails, but currently the page just hangs while it processes - I'd like to instantly thank the user and move on...

any ideas?

11-06-2006, 03:53 AM
You can try sending a header('Location: thankyou.php'); without exitting. I think that will let your script continue to run while the user moves on.

You could also try to spawn a new thread or process either using exec() or the shell cmd function (can't think of it off the top of my head). Good luck with the mail server!

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum