2012-10-18 34 views
1

我遇到了此電子郵件表單的問題。沒有PHP的錯誤代碼或任何東西,電子郵件只是從來沒有交付......這整個代碼示例是在我的網站的幾個頁面上的包含文件。表單會提交給它所在的任何頁面,並且工作,除非我不知道爲什麼它不會發送電子郵件。它似乎沒有處理包含代碼發送電子郵件的else語句。PHP郵件腳本實際上並未發送郵件

<div class="green_box" id="contact_us"> 
<h3>CONTACT US</h3> 
<div class="white_box"> 

<?php 
if ($_POST['submitted']==1) { 
    $errormsg = ""; //Initialize errors 
    if ($_POST[your_name]){ 
     $your_name = $_POST[your_name]; 
    } 
    else { 
     $errormsg = "You did not enter your Name"; 
    } 
    if ($_POST[your_email]){ 
     $your_email = $_POST[your_email]; 
    } 
    else { 
     if ($errormsg){ //If there is already an error, add next error 
      $errormsg = $errormsg . " or your Email"; 
     }else{ 
      $errormsg = "You did not enter your Email"; 
     } 
    } 
    if ($_POST[your_message]){ 
     $your_message = $_POST[your_message]; 
    } 
    else { 
     if ($errormsg){ //If there is already an error, add next error 
      $errormsg = $errormsg . " or your Message"; 
     }else{ 
      $errormsg = "You did not enter your Message"; 
     } 
    } 
    if (strlen($errormsg) > 1) { 
     echo "<p><strong>" . $errormsg . ".</strong><br>Please try again.</p>"; 
    } 
    else { 
     $email_to = "[email protected]"; // recipient inbox 
     $email_subject = "Fore A Partners Website Contact Form"; 
     $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($your_name)."\n"; 
     $email_message .= "Email: ".clean_string($your_email)."\n"; 
     $email_message .= "Comments: ".clean_string($your_message)."\n"; 

     $headers = 'From: '.$your_email."\r\n". 
     'Reply-To: '.$your_email."\r\n" . 
     'X-Mailer: PHP/' . phpversion(); 
     @mail($email_to, $email_subject, $email_message, $headers); 
     echo "<p>Thank you for contacting us. We will be in touch with you very soon.</p>"; 
    } 
} 
?> 

<form name="contactform" method="post"> 
<p>Name<br><input type="text" name="your_name" maxlength="80" size="45"></p> 
<p>Email<br><input type="text" name="your_email" maxlength="80" size="45"></p> 
<p>Message<br><textarea name="your_message" maxlength="1000" rows="6" cols="30"></textarea></p> 
<input type="hidden" name="submitted" value="1"> 
<p><input type="image" src="../btn_submit.png" alt="Submit" name="submit"></p> 
</form> 
</div> 
</div> 
+0

在PHP中,將'@'放在函數前面可以消除錯誤消息 –

+0

您也可以考慮添加代碼if if(empty($ _POST)= ==假){你的代碼}'所以它在正確的零件上執行 – PatomaS

回答

1

UPDATE

爲什麼有可能無法正常工作幾個原因。你在運行什麼類型的服務器?您可能需要在您的php.ini中配置SMTP屬性,但很難說不知道更多。

我個人比較喜歡PEAR郵件解決方案,它更強大,可以讓我輕鬆配置SMTP信息。我清理了你的代碼並實現了一個適用於我的示例PEAR郵件腳本。最有可能你已經PEAR安裝了郵件,但是,如果你需要下載並安裝它去這裏:http://pear.php.net/package/Mail/download

<?php 
if ($_POST) { 
    if ($_POST['submitted']==1) { 
     $errormsg = ""; //Initialize errors 
     if ($_POST['your_name']){ 
      $your_name = $_POST['your_name']; 
     } 
     else { 
      $errormsg = "You did not enter your Name"; 
     } 
     if ($_POST['your_email']){ 
      $your_email = $_POST['your_email']; 
     } 
     else { 
      if ($errormsg){ //If there is already an error, add next error 
       $errormsg = $errormsg . " or your Email"; 
      }else{ 
       $errormsg = "You did not enter your Email"; 
      } 
     } 
     if ($_POST['your_message']){ 
      $your_message = $_POST['your_message']; 
     } 
     else { 
      if ($errormsg){ //If there is already an error, add next error 
       $errormsg = $errormsg . " or your Message"; 
      }else{ 
       $errormsg = "You did not enter your Message"; 
      } 
     } 
     if (strlen($errormsg) > 1) { 
      echo "<p><strong>" . $errormsg . ".</strong><br>Please try again.</p>"; 
     } 
     else { 
      // recipient inbox 
      $email_subject = "Fore A Partners Website Contact Form"; 
      $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($your_name)."\n"; 
      $email_message .= "Email: ".clean_string($your_email)."\n"; 
      $email_message .= "Comments: ".clean_string($your_message)."\n";    

      // INCLUDE PEAR MAIL 
      require_once "Mail.php"; 
      require_once('Mail\mime.php'); 

      // CONFIGURE SMTP SETTINGS 
      $email_to = "[email protected]"; 
      $sender = "[email protected]"; 
      $host = "mail.example.com"; 
      $username = "[email protected]"; 
      $password = "password"; 

      $crlf = "\n";  
      $headers = array(
       'From' => $sender, 
       'To' => $email_to, 
       'Reply-To' => $your_email, 
       'Subject' => $email_subject 
      ); 
      // Creating the Mime message 
      $mime = new Mail_mime($crlf); 
      // Setting the body of the email 
      $mime->setTXTBody($your_message); 
      $mime->setHTMLBody($your_message); 
      $body = $mime->get(); 
      $headers = $mime->headers($headers);  
      // Sending the email 
      $mail =& Mail::factory('smtp', 
      array ('host' => $host, 
       'auth' => true, 
       'username' => $username, 
       'password' => $password 
      )); 
      $mail->send($email_to,$headers,$body); 

      echo "<p>Thank you for contacting us. We will be in touch with you very soon.</p>"; 
     } 
    } 
} 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 
<body> 
<div class="green_box" id="contact_us"> 
<h3>CONTACT US</h3> 
<div class="white_box"> 
<form name="contactform" method="post"> 
<p>Name<br><input type="text" name="your_name" maxlength="80" size="45"></p> 
<p>Email<br><input type="text" name="your_email" maxlength="80" size="45"></p> 
<p>Message<br><textarea name="your_message" maxlength="1000" rows="6" cols="30"></textarea></p> 
<input type="hidden" name="submitted" value="1"> 
<p><input type="image" src="../btn_submit.png" alt="Submit" name="submit"></p> 
</form> 
</div> 
</div> 
</body> 
</html> 

我看到的是你需要抓住自己的帖子,比如$ _ POST的第一件事['your_name '],而不是$ _POST [your_name]

+1

雖然這是一個不好的做法,但它實際上不會導致錯誤。 PHP會故意誤解它被視爲未定義常量的字符串,將'your_name'轉換爲字符串''your_name'',併發出'E_NOTICE'。 –

+0

謝謝傑弗瑞!不幸的是我不熟悉PEAR。 什麼是PEAR頁面沒有幫助任何... http://pear.php.net/manual/en/about.pear.php –

+0

PEAR是一個PHP擴展,您可以測試上面的腳本或使用以下代碼來查看是否安裝了它:<?php phpinfo(INFO_MODULES); ?> –