调试php用于简单的联系表单

时间:2012-04-22 20:14:00

标签: php forms

让我的简单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>

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

插入您的if声明:

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

答案 2 :(得分:0)

你可以尝试

<?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>