【问题标题】:Why does my simple mail php form take a long time to load?为什么我的简单邮件 php 表单需要很长时间才能加载?
【发布时间】:2014-04-21 05:43:04
【问题描述】:

下午好堆栈!我目前正在处理一个简单的 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 " ";
    }
?>

我确实觉得奇怪的一件事是,将我的更改发送给多个收件人开始告诉我错误,即使它工作得很好,所以我改变了..

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

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

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

这是我的代码的上一个版本,可以立即加载和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";
    }
}

?>

感谢您提供任何帮助,因为我已经坚持了一段时间,感谢您的时间和努力,祝大家周末愉快。

【问题讨论】:

  • 较新的版本有一个$subject 的下拉菜单,是动态加载的吗?如果没有,那么您可能想尝试用switch 替换那些if-else 块,看看是否有区别......
  • 邮件($to, $subject, $messageBody, $header);在新版本的 if(sizeof($_POST) && $_POST["name2"] == "") {} 块之外。那怎么办?
  • 这也是带有下拉菜单的页面上的额外javascript。有什么可能运行时间过长吗?
  • @Crackertastic 我已经删除了下拉菜单等的样式,并且似乎没有影响任何负载问题来验证这一点,感谢您的回复!
  • @avist 谢谢 avist 的回复!不确定如何使用开关,但很有用并且会阅读该链接。

标签: javascript php jquery html email


【解决方案1】:
$send_contact = mail($to, $subject, $messageBody, $header);

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

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

【讨论】:

  • 非常感谢!有一个美好的周末。非常感谢您的时间和见解。
  • 没问题。自己也度过一个愉快的周末
猜你喜欢
  • 2017-05-19
  • 1970-01-01
  • 2016-08-03
  • 2014-08-15
  • 2018-07-10
  • 2014-01-15
  • 1970-01-01
  • 2012-06-24
  • 1970-01-01
相关资源
最近更新 更多