Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Slooooow script processing, any help appreciated

    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

    Code:
    // Grab our config settings
    require_once($_SERVER['DOCUMENT_ROOT'].'/config.php');
    
    // Grab the FreakMailer class
    require_once($_SERVER['DOCUMENT_ROOT'].'/lib/MailClass.inc');
    
    $mail = new PHPMailer();
    
    /* grabs the POST variables and puts them into variables that we can use */
    $firstName=$_POST['firstName'];
    $lastName=$_POST['lastName'];
    $email=$_POST['email'];
    $phone=$_POST['phone'];
    $mobile=$_POST['mobile'];
    $fax=$_POST['fax'];
    $address=$_POST['address'];
    $city=$_POST['city'];
    $zipCode=$_POST['zipCode'];
    $heardAbout=$_POST['heardAbout'];
    $inquiring=$_POST['inquiring'];
    $beds=$_POST['beds'];
    $other=$_POST['other'];
    $area=$_POST['area'];
    $maxprice=$_POST['maxprice'];
    
    
    ////////////////////////////////////////  CONNECT TO MYSQL DB  ////////////////////
    // OPEN CONNECTION --->
    $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>"
    	        ."--------CONTACT--------<br>"
                ."First Name: ".$firstName."<br>"
                ."Last Name: ".$lastName."<br>"
                ."E-mail: ".$email."<br>"
                ."<br><br>--------PHONE--------<br>"
                ."Phone: ".$phone."<br>"
                ."Fax: ".$fax."<br>"
                ."Mobile: ".$mobile."<br><br>--------ADDRESS--------<br>"
                ."Street Address: ".$address."<br>"
                ."City: ".$city."<br>"
                ."Post Code: ".$zipCode."<br>"
    			."--------INFO--------<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>"
    			."--------ENDS--------<br><br>";
    	$body .= "*******! <br>";		
    	$body .= "Sincerely, <br>";
        $body .= "********";
    
    
        $mail->From = "***********";
    	$mail->FromName = "***********";
        $mail->Body    = $body;
        $mail->AltBody = $text_body;
        $mail->AddAddress($row["a_email"]);
        if(!$mail->Send())
            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
        $mail->ClearAddresses();
        $mail->ClearAttachments();
      }
    
      
    
    	//--------------------------------------------------
    	
    	
            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"; 
    $FromName="Admin.Team@*******.com";
    $receiptMessage = "Thank you ".$firstName." for using ********!\n\n\nHere is what you submitted to us:\n\n"
                ."--------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"
    			."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");
    //----------------------------------
    
    //  EXECUTE QUERY --->
    $query="INSERT INTO hh1 (
                firstName,
                lastName,
                email,
                phone,
                mobile,
                fax,
                address,
                city,
                zipCode,
                heardAbout,
                inquiringOn,
    			beds,
    			other,
    			maxprice,
    			area)
            VALUES(
                '".$firstName."',
                '".$lastName."',
                '".$email."',
                '".$phone."',
                '".$mobile."',
                '".$fax."',
                '".$address."',
                '".$city."',
                '".$zipCode."',
                '".$heardAbout."',
                '".$inquiring."',
    			'".$beds."',
    			'".$other."',
    			'".$maxprice."',
    			'".$area."')";
    //////----->
    $result=mysql_query($query) or die("Error in query:".mysql_error());
    //if ($result)
       // echo mysql_affected_rows()." row inserted into the database effectively.";
    
    //  CLOSE CONNECTION --->
    mysql_close($connection);
    
    ///////////////////////////////////////////////////////////////////////////////////
            
    ?>
    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •