簡體   English   中英

PHP發送兩封電子郵件

[英]PHP sending two emails

我將這段代碼與其他腳本和在網上找到的東西放在一起,

它以某種方式向我發送了兩封電子郵件,當我剛剛加載頁面時一封電子郵件,而當我提交頁面時第二封電子郵件。

另外, header()並沒有將我發送到我想要的頁面...它只是停留在同一表單頁面上,如果有人可以幫助我了解發生了什么,我將不勝感激確實與自我相關,但我無法為我的愛弄清楚!

謝謝

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<style>
.error {color: #FF0000;}
h6
{
  font-family: bookman old style;
  font-size:20px;
  text-align: center;
  font-weight: normal;
}
h5
{
  font-family: bookman old style;
  font-size:15px;
  text-align: center;
  font-weight: normal;
}
</style>

<?php
$nameErr = $emailErr  = $websiteErr = $categoryErr = "";
$name = $email = $comment  = $website = $category = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   if (empty($_POST["name"])) {
     $nameErr = "Name is required";
   } else {
     $name = test_input($_POST["name"]);
     if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
       $nameErr = "Only letters and white space allowed"; 
     }
   }

   if (empty($_POST["email"])) {
     $emailErr = "Email is required";
   } else {
     $email = test_input($_POST["email"]);
     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
       $emailErr = "Invalid email format"; 
     }
   }

   if (empty($_POST["website"])) {
     $websiteErr = "URL is required";
   } else {
     $website = test_input($_POST["website"]);
     if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
       $websiteErr = "Invalid URL"; 
     }
   }

   if (empty($_POST["comment"])) {
     $comment = "";
   } else {
     $comment = test_input($_POST["comment"]);
   }
   if (empty($_POST["category"])) {
     $categoryErr = "Category is required";
   } else {
     $category = test_input($_POST["category"]);
   }
}

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
?>
<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>
<?php include'header.php'?>
<h6>Link Submission</h6>
<h5><span class="error">* required field.</span>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
   Name Of Site: <input type="text" name="name" value="<?php echo $name;?>">
   <span class="error">* <?php echo $nameErr;?></span>
   <br><br>
   E-mail: <input type="text" name="email" value="<?php echo $email;?>">
   <span class="error">* <?php echo $emailErr;?></span>
   <br><br>
   URL: <input type="text" name="website" value="<?php echo $website;?>">
   <span class="error">* <?php echo $websiteErr;?></span>
   <br><br>
   Description: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
   <br><br>
Category Of Site: <select size="1" name="category"> 
   <option value="<?php echo $category;?>"> -- Please select -- </option>    
   <option>Arts</option>     
   <option>Business</option>     
   <option>Computers</option>     
   <option>Games</option>     
   <option>Health</option>     
   <option>Home</option>     
   <option>Kids and Teens</option>     
   <option>News</option>     
   <option>Recreation</option>     
   <option>Reference</option>     
   <option>Science</option>     
   <option>Shopping</option>     
   <option>Society</option>     
   <option>Sports</option>     
   <option>World</option>

  </select><span class="error">* <?php echo $categoryErr;?></span>
   <br><br>
   <input type="submit" name="submit" value="Submit"> 
</form>
</h5><?php include'footer.php'?>
<?php
$myemail = "links@loadsofads.com";
$subject = "Link Submission";
$message = "Your Link Submission form has been submitted by:
Website Name: $name
E-mail: $email
URL: $website
Category: $category
Description:
$comment";
mail($myemail, $subject, $message);
header('Location: submitthanks.php');
?>

它向您發送兩封電子郵件,因為您需要在條件語句中設置整個代碼。

isset()與您已經命名的“提交”按鈕結合使用,該按鈕僅在單擊“提交”按鈕后才會發送郵件,而不會加載頁面。

<input type="submit" name="submit" value="Submit">

修改為:

<?php

if(isset($_POST['submit'])){

$myemail = "links@loadsofads.com";
$subject = "Link Submission";
$message = "Your Link Submission form has been submitted by:
Website Name: $name
E-mail: $email
URL: $website
Category: $category
Description:
$comment";
mail($myemail, $subject, $message);

header('Location: submitthanks.php');
exit;
}

關於標頭不重定向是因為您在標頭之前輸出,如果已設置/打開錯誤報告 ,則將在標頭之前輸出Headers already sent...警告。

添加ob_start(); 在頁面頂部並在<?php ?>標記內設置有時會有所幫助,並置於<!DOCTYPE html...上方<!DOCTYPE html...

即:

<?php ob_start(); ?>
<!DOCTYPE html ...

您最好在另一個頁面上而不是在同一頁面上使用表單操作,並將郵件代碼放入該文件中。

如果您希望使用當前代碼而不使用第二頁作為郵件處理程序,則另一種選擇是使用元刷新方法。

例如,代替header()

$url = "submitthanks.php";

print "<meta HTTP-EQUIV=Refresh CONTENT=\"0; URL=$url\">";

編輯: -重寫#2

確保更改此行$myemail = "email@example.com"; 作為您的電子郵件地址。

另外,缺少mail()標頭 ,很可能會將郵件發送到垃圾郵件,
並添加了一個from Name因此更具個性化。

<?php

ob_start(); // prevents headers already sent warning

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<style>
.error {color: #FF0000;}
h6
{
  font-family: bookman old style;
  font-size:20px;
  text-align: center;
  font-weight: normal;
}
h5
{
  font-family: bookman old style;
  font-size:15px;
  text-align: center;
  font-weight: normal;
}
</style>

<?php
$nameErr = $emailErr  = $websiteErr = $commentErr = $categoryErr = "";
$name = $email = $comment  = $website = $category = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

   if (empty($_POST["name"])) {

     $nameErr = "Name is required";

$Error = 1;


   } else {
     $name = test_input($_POST["name"]);
     if (!preg_match("/^[a-zA-Z ]*$/",$name)) {

       $nameErr = "Only letters and white space allowed"; 
     }
   }

   if (empty($_POST["email"])) {

     $emailErr = "Email is required";

$Error = 1;


   } else {
     $email = test_input($_POST["email"]);

     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

       $emailErr = "Invalid email format"; 

 $Error = 1;


     }
   }

   if (empty($_POST["website"])) {

     $websiteErr = "URL is required";

$Error = 1;

   } else {
     $website = test_input($_POST["website"]);
     if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {

       $websiteErr = "Invalid URL"; 

     }
   }

   if (empty($_POST["comment"])) {

     $commentErr = "Comment is required";

$Error = 1;

   } else {
     $comment = test_input($_POST["comment"]);
   }

   if ($_POST["category"] == "" ) {

     $categoryErr = "Category is required";

$Error = 1;

   } else {
     $category = test_input($_POST["category"]);
   }

} // brace for if ($_SERVER["REQUEST_METHOD"] == "POST")

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
  }
?>
<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>
<?php  include 'header.php'; ?>
<h6>Link Submission</h6>
<h5><span class="error">* required field.</span>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
   Name Of Site: <input type="text" name="name" value="<?php echo $name;?>">
   <span class="error">* <?php echo $nameErr;?></span>
   <br><br>
   E-mail: <input type="text" name="email" value="<?php echo $email;?>">
   <span class="error">* <?php echo $emailErr;?></span>
   <br><br>
   URL: <input type="text" name="website" value="<?php echo $website;?>">
   <span class="error">* <?php echo $websiteErr;?></span>
   <br><br>
   Description: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea><span class="error">* <br><?php echo $commentErr;?></span>
   <br><br>

Category Of Site: <select size="1" name="category"> 
   <option value="<?php echo $category;?>"> -- Please select -- </option>    
   <option>Arts</option>     
   <option>Business</option>     
   <option>Computers</option>     
   <option>Games</option>     
   <option>Health</option>     
   <option>Home</option>     
   <option>Kids and Teens</option>     
   <option>News</option>     
   <option>Recreation</option>     
   <option>Reference</option>     
   <option>Science</option>     
   <option>Shopping</option>     
   <option>Society</option>     
   <option>Sports</option>     
   <option>World</option>

  </select><span class="error">* <?php echo $categoryErr;?></span>
   <br><br>
   <input type="submit" name="submit" value="Submit"> 
</form>
</h5><?php  include 'footer.php'; ?>


<?php

if(isset($_POST['submit'])){

if ($Error != 1){

$myemail = "email@example.com";
$subject = "Link Submission";
$message = "Your Link Submission form has been submitted by:
Website Name: $name
E-mail: $email
URL: $website
Category: $category
Description:
$comment";

$headers = "From: ". $name . " <" . $email . ">\r\n";

mail($myemail, $subject, $message, $headers);
 header('Location: submitthanks.php');

    } // brace for if ($Error != 1)

} // brace for if(isset($_POST['submit']))
?>

您需要將代碼的這一部分放在發布部分

if ($_SERVER["REQUEST_METHOD"] == "POST"){
    $myemail = "links@loadsofads.com";
    $subject = "Link Submission";
    $message = "Your Link Submission form has been submitted by:
    Website Name: $name
    E-mail: $email
    URL: $website
    Category: $category
    Description:
    $comment";
    mail($myemail, $subject, $message);
    header('Location: submitthanks.php');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM