0

所以我试图让一个联系框工作,它确实有效。但每当有人点击联系页面时,它会自动发送一封空白电子邮件。这要怎么阻止?!

PHP

<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "iamtomalmond@gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='index.html'> Return Home</a>";
?>

HTML

<form action="contact.php" method="POST">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Message</p><textarea name="message" rows="6" cols="25"></textarea><br />
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>

提前致谢 :(

4

3 回答 3

5

您需要检查是否提出了POST请求,并且只有在这种情况下才执行邮件代码:

if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
  // your php code (first block in your question)
}
于 2013-03-18T21:52:17.563 回答
1

您的页面必须$_POST在发送电子邮件之前测试数组的内容:

<?php
   if (!is_empty($_POST)) {
      if (isset($_POST['email']) && isset($_POST['name']) && isset($_POST['message'])) {
          // code to send email as in your question code snippet
      } else {
          // optionally display an error message
      }
   }
 ?>
于 2013-03-18T21:57:17.317 回答
1

那是因为您正在调用mail()页面加载。如果您只想在提交表单时执行此操作(我怀疑),请$_POST在调用之前检查您的某些变量是否存在mail()

if (isset($_POST['foo']))
{
     //do stuff here
}
于 2013-03-18T21:52:36.457 回答