为什么我的简单邮件php表单需要很长时间才能加载?

时间:2014-03-15 22:02:05

标签: javascript php jquery html email

下午好!我目前正在编写一个简单的PHP邮件表单,而我之前的版本在我开始编辑之前花了不到一秒的时间来实际加载表单,但是随着这些新增功能,它现在已经接管了至少5个加载表单10秒钟。

这是我现在的代码,需要很长时间才能加载,这里是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 " ";
    }
?>

有一件事我觉得很奇怪,让我发送给多个reciepents的更改开始告诉我错误,即使它工作得很好,所以我改变了..

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

只打印一个空格,所以用户不会看到它......也许,这可能会导致实际发生的事情延长加载延迟?但是......它的效果非常好而且完美无缺。

任何人都知道为什么这个表单需要很长时间才能加载?它绝对是代码,因为我删除它或恢复到我的旧版本,它在不到一秒的时间内加载ySlow。

以下是我的代码的PREVIOUS版本,可立即加载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";
    }
}

?>

任何帮助都是值得赞赏的,因为我已经坚持了一段时间,感谢您的时间和精力,并为每个人度过美好的周末。

1 个答案:

答案 0 :(得分:1)

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

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

这部分代码被排除在处理新版本表单提交的if块之外。因此每次加载页面时都会执行它。这将减慢加载速度,因为mail()函数会尝试发送电子邮件并在每次加载页面时都会出错。您的意图是仅在表单正确提交时发送电子邮件。从那以后打开php通知就是一个好主意。它会使这样的错误变得容易。