[英]Why does my simple mail php form take a long time to load?
下午好,堆栈! 我目前正在处理一个简单的PHP邮件表单,而在开始编辑之前,我的上一个版本实际上花了不到一秒钟的时间来实际加载表单,但是现在有了这些新功能,现在至少需要5到10秒钟才能加载该表单。 。
这是我当前的代码,现在加载需要很长时间,这是一个实时链接 ;
<?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加载不到一秒钟。
这是我的代码的上一个版本,可立即加载并带有实时链接 ;
<?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";
}
}
?>
感谢您的帮助,因为我已经坚持了一段时间,感谢您的时间和精力,祝大家周末愉快。
$send_contact = mail($to, $subject, $messageBody, $header);
if($send_contact){
$mail_sent = true;
}
else {
echo " ";
}
这部分代码未包含在处理新版本中的表单提交的if块中。 因此,它在页面每次加载时都会执行。 这将减慢加载速度,因为mail()函数尝试发送电子邮件并在每次加载页面时给出错误。 您的意图是仅在正确提交表单后才发送电子邮件。 从那以后,打开php通知将是一个好主意。 这样可以更轻松地捕获此类错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.