SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah im guessing sending the emails are whats holding you up, although that sounds like a long time to send 3 emails.

    use the microtime() function to test the time needed to execute different blocks of your code. that will help you identify the problem area(s)

  3. #3
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy

    Hey thanks, I've didn't know about that function.

    Here are my results:

    First part, to mysqlconnect took 0.0062239170074463s
    sending mail to agent took 16.591768980026s
    sending mail to USER took 21.099683046341s
    INSERT QUERY took 0.0003819465637207s

    Sending mail takes a really long time! The mysql stuff is instant.

    The fastest I have timed the total processing is 50 secs, it sometimes takes 1m15 - this is way too long.

    I need to think of a way to have the response go back to the user and have the mail processing in the background. It doesn't matter if the mail goes out half an hour later, but I can't have the user sitting looking at a screen with an eggtimer for that long.

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thats very slow, sending an email should be pretty quick usually.

    an immediate work around would be to create a mail queue. store the emails to be sent in a database. then have another script run via a cron job which will send any queued emails out. but this is really a bandaid for something else that is wrong.


    been a while since i used phpmailer(i like swiftmailer better) but i think phpmailer by default uses the mail() function internally, but has an option to connect directly to smtp instead. you might want to try that to see if theres any difference.

  5. #5
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,198
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    Try dropping the phpmailer, and just use a normal mail syntax to send them. My guess is that the slowdown is in that script.

    Regarding the mail command when its run on a unix based os it will que the emails. I.e. if you send several fast after eachother they will be qued automatically for beeing sent. This actually means that you might get a "success" message on the email you sent, but it can actually fail in the email que

  6. #6
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I dumped the first lot of emails (the phpmailer ones) from the script temporarily, leaving in just an ordinary mail send to the user, to confirm their input.

    I still get this:
    sending mail to USER took 13.929791927338s
    whole script took 13.930603981018s

    It's so frustrating. I'm thinking of not having an email confirmation, and just giving it on screen instead. But I don't know where to start looking to find out why emails take so long to process.

  7. #7
    Working on it... Contrid's Avatar
    Join Date
    Apr 2006
    Location
    Online
    Posts
    955
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Are you using SMTP authentication?

    Edit :

    Quickly scanned through the code and it seems like you don't.

    You'll have to contact your host about this. I used to have the same problem with my ISP when sending emails with phpMailer...but I have to use authentication, so it might be a littlebit different.
    And so I got lost in code...completely asphyxiated by it...

    Premium WordPress plugins - Tribulant Software

  8. #8
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I couldn't use SMTP, my ISP always rejects it, both with and without authentication.

  9. #9
    Working on it... Contrid's Avatar
    Join Date
    Apr 2006
    Location
    Online
    Posts
    955
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Does it make a difference if you assign the phpMailer class varialbe "Host" ?
    And so I got lost in code...completely asphyxiated by it...

    Premium WordPress plugins - Tribulant Software

  10. #10
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you running this code from your local machine, or your live web server?
    When I send mail from PHP locally their is a lag while PHP communicates with my ISP. However it is no-where near this long (maybe 2-4 seconds).

    Also, Swiftmailer does perform better than PhpMailer, but I doubt that is your issue. PhpMailer definitely not THIS slow!


Bookmarks

Posting Permissions

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