2014-11-21 92 views
-2

我正在爲網站創建一個電子郵件表單,其html代碼如下。PHP電子郵件不起作用

 <form class="form" name="htmlform" method="post" action="contact_process.php"> 
     <table width="450px"> 
     </tr> 
     <tr> 
     <td valign="top"> 
      <label for="first_name">First Name *</label> 
     </td> 
     <td valign="top"> 
      <input type="text" name="first_name" maxlength="50" size="30"> 
     </td> 
     </tr> 

     <tr> 
     <td valign="top""> 
      <label for="last_name">Last Name *</label> 
     </td> 
     <td valign="top"> 
      <input type="text" name="last_name" maxlength="50" size="30"> 
     </td> 
     </tr> 
     <tr> 
     <td valign="top"> 
      <label for="email">Email Address *</label> 
     </td> 
     <td valign="top"> 
      <input type="text" name="email" maxlength="80" size="30"> 
     </td> 

     </tr> 
     <tr> 
     <td valign="top"> 
      <label for="telephone">Telephone Number</label> 
     </td> 
     <td valign="top"> 
      <input type="text" name="telephone" maxlength="30" size="30"> 
     </td> 
     </tr> 
     <tr> 
     <td valign="top"> 
      <label for="comments">Comments *</label> 
     </td> 
     <td valign="top"> 
      <textarea name="comments" maxlength="1000" cols="25" rows="6"></textarea> 
     </td> 

     </tr> 
     <tr> 
     <td colspan="2" style="text-align:center"> 
      <input type="submit" value="Submit"> 
     </td> 
     </tr> 
     </table> 
     </form> 
     <p>* Mandatory fields</p> 

嘗試了很多php代碼(在contact_process.php中),但沒有通過電子郵件發送到指定的電子郵件。

<?php 
if(isset($_POST['email'])) { 

    $email_to = "[email protected]"; 

    $email_subject = "Feedback Form Submissions"; 


    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 go back and fix these errors.<br /><br />"; 
     die(); 
    } 

    // validation expected data exists 
    if(!isset($_POST['first_name']) || 
     !isset($_POST['last_name']) || 
     !isset($_POST['email']) || 
     !isset($_POST['telephone']) || 
     !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 
    $last_name = $_POST['last_name']; // required 
    $email_from = $_POST['email']; // required 
    $telephone = $_POST['telephone']; // 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 First Name you entered does not appear to be valid.<br />'; 
    } 
    if(!preg_match($string_exp,$last_name)) { 
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />'; 
    } 
    if(strlen($comments) < 2) { 
    $error_message .= 'The Comments 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 .= "Last Name: ".clean_string($last_name)."\n"; 
    $email_message .= "Email: ".clean_string($email_from)."\n"; 
    $email_message .= "Telephone: ".clean_string($telephone)."\n"; 
    $email_message .= "Comments: ".clean_string($comments)."\n"; 


// creating 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 contacting us. We will be in touch with you very soon. 

<?php 
} 
die(); 
?> 

有人可以檢查代碼,我做錯了什麼。

此外,如果可能出現安全問題,我想包括一個自動回覆發件人和CAPTCHA圖片。

+1

任何錯誤訊息? 「不起作用」是什麼意思? – panther 2014-11-21 07:46:41

+0

您正在將**三個**問題捆綁爲一個:(1)爲什麼PHP「mail()」無法正常工作,哪個** [實際上](http://stackoverflow.com/questions/8803994/php-mail-因爲某些原因而不工作)[has](http://stackoverflow.com/questions/20297703/mail-function-is-not-working-in-php)[been](http:// stackoverflow。 com/questions/23045613/my-php-mail-function-is-not-working)[問](http://stackoverflow.com/questions/9883755/php-mail-function-not-sending-mail)[before ](http://stackoverflow.com/questions/21961821/send-mail-by-php-mail-function)**,以及(2)如何整合自動響應和(3)驗證碼。 – Terry 2014-11-21 07:50:33

+0

@WebDesign是否檢查函數郵件的返回值? – Riccardo 2014-11-21 07:54:15

回答

1

如果您檢查郵件以查看郵件是否被髮送,這很方便。這是繼續以下內容

if (mail ($email_to, $email_subject, $email_message, $headers)) { 
    echo "mail send"; 
} else { 
    echo "failure to send"; 
} 

它在if語句中運行郵件函數,如果您看到是否有錯誤,則會生成郵件函數。如果是這樣,你必須進行調試才能找出錯誤是什麼,因爲你在那裏有幾個變量。這可能只是您變量中的一個格式問題。

編輯

而且,也許這是我的,但這並不意義,我在你的代碼。

$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 .= "Last Name: ".clean_string($last_name)."\n"; 
$email_message .= "Email: ".clean_string($email_from)."\n"; 
$email_message .= "Telephone: ".clean_string($telephone)."\n"; 
$email_message .= "Comments: ".clean_string($comments)."\n"; 

爲什麼你想檢查你的字符串內容類型,密件副本等的附加標籤...並替換成什麼。我的意思是......爲什麼首先要添加它,以及你在哪裏添加了這些內容。我沒看到它,是嗎?

這是不好的,甚至可能是因爲我試圖在註釋中放置複製代碼的錨點,這會自動移除,因爲您不會允許href屬性。如果我是一個客戶,他會在代碼中看到一個錯誤,並希望將它指向您並將代碼複製過來?這只是順帶思考,我的POV。

編輯2

我也希望你有這段代碼if(isset($_POST['email'])) {如果電子郵件變量設置你bassically檢查實現。因此,如果我只填寫一封電子郵件並運行它,整個腳本就會執行。你最好能在提交行動上做到這一點。你還沒有爲它命名,所以你可能想這樣做。

<input type="submit" value="Submit"> - ><input type="submit" name="SubmitEmail" value="Submit">

然後檢查其

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