电子邮件不会发送给联系表单提交(PHP)

时间:2016-11-15 07:27:15

标签: php html

有人可以帮我弄清楚我错在哪里吗?单击“发送”后,表单将转到PHP代码。我已经搞乱了好几个小时,并认为我只是在做一些非常简单的事情。稍微离开这一点然后回到漩涡中。但与此同时,如果有人能帮助我,请告诉我。

这是HTML



<div class="container">
  <div class="row form-container">

    <div class="col-md-12 contact-form">
      <h3 style="text-align:center">Rather have us contact you? No problem!</h3>
      <h4 style="text-align:center">Just fill out this form.</h4>
      <form name="contactform" method="post" action="send_form_email.php">
        <div class="form-group">
          <input type="text" class="form-control" id="name" name="name" placeholder="Your Name..." value="" required>
        </div>
        <div class="form-group">
          <input type="text" class="form-control" id="email" name="email" placeholder="Your email..." value="" required>
        </div>
        <div class="form-group">
          <input type="text" class="form-control" id="phone" name="phone" placeholder="Your phone..." value="">
        </div>
        <div class="form-group">
          <textarea class="form-control" rows="4" name="message" placeholder="Your message..."></textarea>
        </div>
        <div class="form-group">
          <button type="submit formnovalidate" name="submit" value="Submit" class="btn btn-default"><i class="fa fa-paper-plane fa-fw"></i> Send</button>
        </div>
      </form>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

这是PHP

<?php

if(isset($_POST['email'])) {





$email_to = "mdevlin14@gmail.com";

$email_subject = "Contact from Hebert Counseling";





function died($error) {



    echo "We are very sorry, but there were error(s) found with the     form you submitted. ";

    echo "These errors appear below.<br /><br />";

    echo $error."<br /><br />";

    echo "Please go back and fix these errors.<br /><br />";

    die();
}



if(!isset($_POST['name']) ||

    !isset($_POST['email']) ||

    !isset($_POST['phone']) ||

    !isset($_POST['message'])) {

    died('We are sorry, but there appears to be a problem with the form you submitted.');

}



$first_name = $_POST['name']; // required

$email_from = $_POST['email']; // required

$telephone = $_POST['phone']; // not required

$comments = $_POST['message']; // not required



$error_message = "";

$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

if(!preg_match($email_exp,$email_from)) {

$error_message .= 'The Email Address you entered does not appear to be valid.<br />';

}

$string_exp = "/^[A-Za-z .'-]+$/";

if(!preg_match($string_exp,$name)) {

$error_message .= 'The name you entered does not appear to be valid.<br />';

}

if(strlen($message) < 2) {

$error_message .= 'The message you entered do not appear to be valid.  <br />';

}

if(strlen($error_message) > 0) {

died($error_message);

}

$email_message = "Form details below.\n\n";



function clean_string($string) {

  $bad = array("content-type","bcc:","to:","cc:","href");

  return str_replace($bad,"",$string);

}



$email_message .= "Name: ".clean_string($first_name)."\n";

$email_message .= "Email: ".clean_string($email_from)."\n";

$email_message .= "Phone: ".clean_string($telephone)."\n";

$email_message .= "Message: ".clean_string($comments)."\n";





//  email headers

$headers = 'From: '.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);

?>





Thank you for reaching out! We will be in touch with you very soon!



<?php

}

?>

1 个答案:

答案 0 :(得分:0)

正如Franz所指出的, mail()函数之前肯定删除错误抑制(@符号)。然后,您将看到现在不可见的警告和错误,您可以解决问题。

您可以通过 php.ini 中设置 mail.log 路径来检查是否实际发送了任何电子邮件:

; The path to a log file that will log all mail() calls. Log entries include
; the full path of the script, line number, To address and headers.
mail.log = "C:\xampp\mail.log"

您还可以将所有电子邮件重定向到文件,而不是将其真实发送。在Windows上的XAMPP中,有一个脚本mailtodisk.exe。只需在php.ini中设置:

sendmail_path = "C:\xampp\mailtodisk\mailtodisk.exe"

您会在C:\xampp\mailoutput文件夹中找到已发送的电子邮件。

请勿忘记在任何更改后重新启动服务器。