简体   繁体   中英

PHP mail() is not sending email

I am using the following code for a contact form in HTML:

  <form method="post" name="contact_form"
action="contact-form-handler.php" data-add-back-btn="false" data-ajax="false">
    Your Name:
      <span id="sprytextfield1">
      <input type="text" name="name">
      <span class="textfieldRequiredMsg">A value is required.</span></span> 
      Code:
      <span id="sprytextfield1">
      <input type="text" name="code">
      <span class="textfieldRequiredMsg">A value is required.</span></span> 
      Email Address:
      <span id="sprytextfield2">
      <input type="text" name="email">
      <span class="textfieldRequiredMsg">A value is required.</span></span> Message:
<textarea name="message"></textarea>
    <input type="submit" value="Submit">
</form>

That is the PHP file to handle the mail function:

<?php
$errors = '';
$myemail = 'email';//<-----Real email address here.
if(empty($_POST['name'])  ||  empty($_POST['code'])  ||
   empty($_POST['email']) ||
   empty($_POST['message']))
{
    $errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$code = $_POST['code'];
$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))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email_address\n Message \n $message".
"Codigo: $code\n";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>

When the user completes the form and clicks on the submit button, the thank you page is opened, but the form isn't emailed.

Any help is welcome.

如果mail()返回true ,则联系托管人的客户服务

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