简体   繁体   中英

Why does my simple mail php form take a long time to load?

Good afternoon Stack! I'm currently working on a simple PHP mailing form and my previous version before I started editing it took less than a second to actually load the form, however with these new additions it's now taking over at least 5-10 seconds to load the form.

This is my current code now that takes pretty long to load and here is a LIVE LINK ;

<?php
// We use the name2 field as bait for spambots.  It's display is off,
// so humans wouldn't know to enter anything into it.  Robots would, 
//  so we ignore submissions with info in name2.

$mail_sent = false;

if(sizeof($_POST) && $_POST["name2"] == "") // receiving a submission
{   

 // receiving a submission
    $to = $_POST['department'];

    // prep our data from the form info
    $senderName     = $_POST['name'];
    $senderEmail    = $_POST['email'];
    $department     = $_POST['department'];
    $subject        = "Visitor message in $department department"; 
    $messageBody    = $senderName . ' ('.$senderEmail.') wrote for '.$department.' department: ' . $_POST['message'];

    if($department == 'customer') { //if customer was selected
    $to = 'email@email.com';
    }

    else if($department == 'distribution') { //if distribution was selected
    $to = 'email@email.com, email@email.com';
    }

    else if($department == 'press') { //if press was selected
    $to = 'email@email.com';
    }

    else if($department == 'career') { //if career was selected
    $to = 'email@email.com';
    }

    else if($department == 'other') { //if other was selected
    $to = 'email@email.com';
    }

    // From 
    $header = "From: $senderName <$senderEmail>";
}

    // Send it!

    $send_contact = mail($to, $subject, $messageBody, $header);

    if($send_contact){
        $mail_sent = true;
    }
    else {
        echo " ";
    }
?>

One thing I did find weird though, making my changes to send to multiple reciepents started telling me ERROR even though it worked perfectly fine, so I changed..

if($send_contact){
    $mail_sent = true;
}
else {
    echo "ERROR";
}

to print nothing but a space, so users don't see it.. Maybe, this is what could cause it that something is actually going on which prolongs the delay of loading? But.. It works fantastic and perfectly fine.

Anyone have any clue why this form can take so long to be loading? It's definitely the code as I removed it or reverted back to my old version, it loads in less than a second with ySlow.

Here is the PREVIOUS version of my code that loads instantly and a LIVE LINK ;

<?php

// We use the name2 field as bait for spambots.  It's display is off,
// so humans wouldn't know to enter anything into it.  Robots would, 
//  so we ignore submissions with info in name2.

$mail_sent = false;

if(sizeof($_POST) && $_POST["name2"] == "") // receiving a submission
{   
    define("SUBJECT",   "Visitor Message from Domain.com");

    // production recipient:
    define("RECIPIENT", "email@email.com");

    // prep our data from the form info
    $senderName     = $_POST['name'];
    $senderEmail    = $_POST['email'];
    $subject        = SUBJECT; 
    $messageBody    = $senderName . ' ('.$senderEmail.') wrote:

' . $_POST['message'];

    // From 
    $header         = "from: $senderName <$senderEmail>";

    // To
    $to = RECIPIENT;

    // Send it!
    $send_contact = mail($to, $subject, $messageBody, $header);

    // Check success of send attempt
    if($send_contact){
        // show thankyou screen
        $mail_sent = true;
    }
    else {
        // send failed.
        echo "ERROR";
    }
}

?>

Any help is kindly appreciated as I've been stuck on this for awhile, thank you for your time and efforts and have a wonderful weekend everyone.

$send_contact = mail($to, $subject, $messageBody, $header);

if($send_contact){
    $mail_sent = true;
}
else {
    echo " ";
}

This part of the code was left out of the if block that handles your form submission in your new version. So it gets executed every time the page gets loaded. That will slow down the loading because the mail() function tries to send an email and gives an error every time you load the page. Your intention was to send an email only when the form gets correctly submitted. Turning on php notices would be a good idea since. It would make catching errors like this easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM