簡體   English   中英

電子郵件不會發送聯系表格提交(PHP)

[英]Email won't send on contact form submission (PHP)

有人能幫我弄清楚我錯在哪里嗎? 單擊“發送”時,表單將轉到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> 

這是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

}

?>

正如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文件夾中找到已發送的電子郵件。

不要忘記在任何更改后重新啟動服務器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM