2012-04-22 74 views
2

有一個問題讓我簡單的PHP電子郵件聯繫表格工作。沒有錯誤生成或返回,但我沒有收到任何電子郵件。調試php的簡單聯繫表格

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

而且我使用的PHP(我當然是正確的電子郵件)

任何人都可以看到下面的表單代碼的任何錯誤? 什麼是一些通用的調試步驟來嘗試? 在此先感謝!

<?php 
$errors = ''; 
$myemail = '[email protected]';//<-----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> 
+2

嗯......我看不到任何實際的'郵件()'調用,因此這就是爲什麼你沒有收到任何電子郵件... – stealthyninja 2012-04-22 20:16:08

+1

好吧..哪裏是應該發送電子郵件的電子郵件()函數(或類似的)的調用? – 2012-04-22 20:16:11

回答

0

您可以嘗試

<?php 
$errors = array(); 
$myemail = '[email protected]'; // <-----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> 
+0

這是不完全正確的,但if(@mail($ to,$ subject,$ message,$ headers)){讓我走在正確的軌道上 – 2012-04-26 21:22:46

0

插入此betwin您if聲明:

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