我按照this page上的说明为我的联系表单设置了验证。表单发送电子邮件但仍返回“错误!”信息。我认为问题出在Mail.PHP文件中,因为错误信息是die函数中使用的错误信息
PHP验证
<?php
if(isset( $_POST['contact-name']))
$name = $_POST['contact-name'];
if(isset( $_POST['contact-email']))
$email = $_POST['contact-email'];
if(isset( $_POST['contact-tel']))
$tel = $_POST['contact-tel'];
if(isset( $_POST['contact-subject']))
$subject = $_POST['contact-subject'];
if(isset( $_POST['contact-message']))
$message = $_POST['contact-message'];
if ($name === ''){
echo "Name cannot be empty.";
die();
}
if ($email === ''){
echo "Email cannot be empty.";
die();
} else {
if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
echo "Email format invalid.";
die();
}
}
if ($tel === ''){
echo "Phone number cannot be empty.";
die();
}
if ($subject === ''){
echo "Subject cannot be empty.";
die();
}
if ($message === ''){
echo "Message cannot be empty.";
die();
}
$content="From: $name \n Email: $email \n Phone number:$tel \n Message: $message";
$recipient = 'kimpope1@cox.net';
$header = 'BCC: jonwashdesigns@gmail.com'.PHP_EOL;
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $content, $mailheader, $header) or die("Error!");
echo "Email sent!";
?>
答案 0 :(得分:0)
而不是使用
mail($recipient, $subject, $content, $mailheader, $header) or die("Error!");
使用
$status = mail($recipient, $subject, $content, $mailheader, $header);
echo $status ? 'Mail sent' : 'Error sending mail';