PHP mail()不发送电子邮件

时间:2014-05-27 18:58:31

标签: php html html-form

我在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>

这是处理邮件功能的PHP文件:

<?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');
}
?>

当用户完成表单并单击提交按钮时,会打开感谢页面,但不会通过电子邮件发送表单。

欢迎任何帮助。

1 个答案:

答案 0 :(得分:1)

如果mail()返回true,请联系您的主机客户服务