2

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

3 回答 3

0

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

于 2012-04-22T20:17:17.733 回答
0

if在你的陈述之间插入这个:

if(empty($errors)) {
    mail($email,$subject,$message,$headers);
    header('Location: contact-form-thank-you.html');
} 
于 2012-04-22T20:19:13.990 回答
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>
于 2012-04-22T20:28:51.047 回答