[英]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>
您應該添加對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');
}
你可以試試
<?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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.