调试 PHP 以获得简单的联系表单


debugging php for simple contact form

在让我简单的 PHP 电子邮件联系表单工作时遇到问题。没有生成或返回任何错误,但我没有收到任何电子邮件。

这是我的形式 http://bitstream.ca/beta2/contact.html

还有我

正在使用的 php(当然还有我正确的电子邮件)

任何人都可以看到下面的表单代码有任何错误吗?可以尝试哪些常规调试步骤?提前感谢!

<?php 
$errors = '';
$myemail = 'foo@foo.foo';//<-----Put Your email address here.
if(empty($_POST['name'])  || 
   empty($_POST['email']) || 
   empty($_POST['message']))
{
    $errors .= "'n Error: all fields are required";
}
$name = $_POST['name']; 
$email_address = $_POST['email']; 
$message = $_POST['message']; 
if (!preg_match("/^[_a-z0-9-]+('.[_a-z0-9-]+)*@[a-z0-9-]+('.[a-z0-9-]+)*('.[a-z {2,3})$/i", $email_address))
  {
  $errors .= "'n Error: Invalid email address";
  }
  if( empty($errors))
  {
    header('Location: contact-form-thank-you.html');
  } 
 ?>
<!DOCTYPE HTML> 
<html>
 <head>
 <title>Contact form handler</title>
 </head>
<body>
<!-- This page is displayed only if there is some error -->
<?php
   echo nl2br($errors);
?>
 </body>
</html>
你可以

试试

<?php
$errors = array ();
$myemail = 'foo@foo.foo'; // <-----Put Your email address here.
$name = $_POST ['name'];
$to = $_POST ['email'];
$message = $_POST ['message'];
$subject = "Sample Email";
$headers = "From: $myemail" . "'r'n" . "Reply-To: $myemail" . "'r'n" . 'X-Mailer: PHP/' . phpversion ();
if (empty ( $_POST ['name'] ) || empty ( $_POST ['email'] ) || empty ( $_POST ['message'] )) {
    $errors [] = "Error: all fields are required";
}
if (! preg_match ( "/^[_a-z0-9-]+('.[_a-z0-9-]+)*@[a-z0-9-]+('.[a-z0-9-]+)*('.[a-z {2,3})$/i", $to )) {
    $errors [] = "Error: Invalid email address";
}
if (count ( $errors ) == 0) {
    if (@mail ( $to, $subject, $message, $headers )) {
        $errors [] = "Can't send Email";
    }
    header ( 'Location: contact-form-thank-you.html' );
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Contact form handler</title>
</head>
<body>
    <!-- This page is displayed only if there is some error -->
<?php
echo implode ( "<br />", $errors );
?>
 </body>
</html>

你应该添加一个对mail()函数(或其他一些邮件PEAR类或类似类)的调用来实际发送电子邮件。看这里: http://php.net/manual/en/function.mail.php

插入您的if语句:

if(empty($errors)) {
    mail($email,$subject,$message,$headers);
    header('Location: contact-form-thank-you.html');
}