简体   繁体   中英

Contact form sending emails but returning error message

I followed the instructions on this page to setup validation for my contact form. The form sends an email but still returns an "Error!" message. I believe that the problem is with the Mail.PHP file, because the error message is is the one used in the die function

PHP validation

<?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!";
?>

在此处输入图片说明

Rather than using

mail($recipient, $subject, $content, $mailheader, $header) or die("Error!");

use

$status = mail($recipient, $subject, $content, $mailheader, $header);
echo $status ? 'Mail sent' : 'Error sending mail';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM