1

我做了一个简单的联系表格,只是为了练习,但由于某种原因它不起作用。这是我的代码:

 <?php
 if($_SERVER['REQUEST_METHOD'] == "POST") {

 $required = array('name', 'email', 'onderwerp', 'bericht');

 $error = false;
 foreach($required as $field) {
   if (empty($_POST[$field])) {
     $error = true;
   }
  }

 if ($error) {
   echo "All fields are required!";
 } else {
  $to = 'example@example.com';
  $datum = date("d-m-Y H:i"); 
  $ip = $_SERVER['REMOTE_ADDR'];
  $naam = $_POST['name'];
  $email = $_POST['email'];
  $from = $_POST['email'];
  $onderwerp = $_POST['onderwerp'];
  $bericht = $_POST['bericht'];
  $subject = "New e-mail!: $onderwerp";
  $message .= $bericht . "\n\n"; 
  $message .= "-----------------------------\n"; 
  $message .= "From:: " . $name . " (" . $_POST['email'] . ")\n"; 
  $message .= "IP-adres: " . $ip . "\n"; 
  $message .= "Sent on $datum \n"; 
  $headers = 'From: '.$_POST['email'];

 $sent_email = mail($to, $subject, $message, $headers);
 if (!$sent_email) {
 echo "Mail has been sent!";
 }
 else
 {
 echo "Woops!";
 }

 }

 }
 else {
// Form has not been submitted
 }

所以结果是糟糕!但我不知道为什么它不起作用:(提前谢谢!!

4

2 回答 2

1

您正在if错误地检查语句。[实际上邮件是在您的情况下发送的]您的代码应该是。

 if ($sent_email) {  // I have modified here 
 echo "Mail has been sent!";
 }
 else
 {
 echo "Woops!";
 }
于 2013-10-24T19:39:22.873 回答
0

你的逻辑是落后的。mail()成功时返回 TRUE。但是您将 TRUE 视为失败。如果应该

if ($sent_email === TRUE) {
   success ...
} else {
   failure ...
}
于 2013-10-24T19:40:18.303 回答