[英]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.