2016-12-30 52 views
-2

所以,兩個問題:用PHP正確編碼電子郵件表格?

1.)我的電子郵件表單不工作,在提交時,它建議「網頁無法顯示」。

2.)電子郵件表格在正確編碼時並未發送電子郵件。

我試圖讓我的電子郵件表單正常工作。這是工作,(但不是發送電子郵件),然後我重新安排了一些東西(在表格中添加主題行)。現在,我似乎無法弄清楚一旦我添加主題行後會出現什麼問題。有什麼建議麼?

https://jsfiddle.net/ebxam743/1/

我的PHP形式是的jsfiddle的CSS部分。

聯繫表HTML

<div class="col-md-8"> 
     <form name="contactform" method="post" action="send_form_email.php"> 

      <div class="row contact-row"> 
      <div class="col-md-6 contact-name"> 

       <input type="text" name="first_name" placeholder="Name"> 
      </div> 

      <div class="col-md-6 contact-email"> 

       <input type="text" name="email" placeholder="E-mail*"> 
      </div> 
      </div> 

      <input type="text" name="subject" placeholder="Subject*"> 

      <textarea name="comments" placeholder="Message"></textarea> 
      <input type="submit" class="btn btn-lg btn-color btn-submit" value="Send Message"> 
      </div> 
     </div> 
     </form> 
     <!-- end col --> 

PHP(不能得到正常的格式)...

<?php 

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



    // EDIT THE 2 LINES BELOW AS REQUIRED 

    $email_to = "[email protected]"; 

    $email_subject = "Your email subject line"; 





    function died($error) { 

     // your error code can go here 

     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 <a href="contact.html">go back</a> and fix these errors.<br /><br />"; 

     die(); 

    } 



    // validation expected data exists 

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

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

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

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

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

    } 



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

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

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

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



    $error_message = ""; 

    $email_exp = '/^[A-Za-z0-9._%-][email protected][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,$first_name)) { 

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


    } 

    if(strlen($comments) < 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 .= "First Name: ".clean_string($first_name)."\n"; 

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

    $email_message .= "Subject: ".clean_string($subject)."\n"; 

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





// create 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); 

?> 



<!-- include your own success html here --> 


<center><img src="img/rebelliouslogob.png" 

      Thank you for contacting us. We will be in touch with you very soon.</center> 

    <a href="contact.html"><h1>Go Back</h1></a> 



<?php 

} 

?> 
+0

那麼,如果您不發佈您的PHP代碼,您如何期待我們的幫助?請注意,指向某個任意網站的鏈接不代替在問題中內聯發佈代碼。您需要發佈代碼的相關部分。 – arkascha

+0

好吧,我不能讓它發佈沒有它看起來在代碼選項wonky .... – adr5194

+0

然後,你應該理清它的外觀。 _不發佈它不是一個答案,是嗎? – arkascha

回答

4

好了,所以我找到了2個錯誤。

第一個錯誤

這條線:由於報價符號是打破字符串

echo "Please <a href=\"contact.html\">go back</a> and fix these errors.<br /><br />"; 

,所以如果你需要使用:

echo "Please <a href="contact.html">go back</a> and fix these errors.<br /><br />"; 

應改爲在字符串內引用符號,您需要通過添加反斜槓或使用單引號創建字符串來將其轉義。

二錯誤

您指的是一個變量,名爲$主題不存在。

因此,而不是使用一個名爲$電話變量,它包含主題的數據,它的名稱更改爲$主題(你永遠不使用變量$電話,所以它不應該影響到你)

這條線:

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

應改爲:

$subject = $_POST['subject']; // not required 

編輯:測試的代碼我的服務器上後,這兩個變化我上面提到的,我收到一封電子郵件,如預期。

+0

謝謝你的幫助!有效。 – adr5194