2014-03-27 201 views
0

嗨我已经下载了刷牙敏感的网站模板和PHP联系表单不会工作。电子邮件不能发送与PHP

这是接触形式怎么看起来像index.html文件

<div class="row"> 
    <div class="span9"> 
    <form id="contact-form" class="contact-form" action="_include/php/contact.php"> 
     <p class="contact-name"> 
     <input id="contact_name" type="text" placeholder="Full Name" value="" name="name" /> 
     </p> 
     <p class="contact-email"> 
     <input id="contact_email" type="text" placeholder="Email Address" value="" name="email" /> 
     </p> 
     <p class="contact-message"> 
     <textarea id="contact_message" placeholder="Your Message" name="message" rows="15" cols="40"></textarea> 
     </p> 
     <p class="contact-submit"> 
     <a id="contact-submit" class="submit" href="#">Send Your Email</a> 
     </p> 
     <div id="response"></div> 
    </form> 
    </div> 

    <div class="span3"> 
    <div class="contact-details"> 
     <h3>Contact Details</h3> 
     <ul> 
     <li><a href="#">[email protected]</a></li> 
     <li>(916) 375-2525</li> 
     <li> 
      Brushed Studio 
      <br> 
      5240 Vanish Island. 105 
      <br> 
      Unknow 
     </li> 
     </ul> 
    </div> 
    </div> 
</div> 

这该是多么的PHP样子

<?php 

$admin_email = '[email protected]'; // Your Email 
$message_min_length = 5; // Min Message Length 


class Contact_Form{ 
    function __construct($details, $email_admin, $message_min_length){ 

     $this->name = stripslashes($details['name']); 
     $this->email = trim($details['email']); 
     $this->subject = 'Contact from Your Website'; // Subject 
     $this->message = stripslashes($details['message']); 

     $this->email_admin = $email_admin; 
     $this->message_min_length = $message_min_length; 

     $this->response_status = 1; 
     $this->response_html = ''; 
    } 


    private function validateEmail(){ 
     $regex = '/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i'; 

     if($this->email == '') { 
      return false; 
     } else { 
      $string = preg_replace($regex, '', $this->email); 
     } 

     return empty($string) ? true : false; 
    } 


    private function validateFields(){ 
     // Check name 
     if(!$this->name) 
     { 
      $this->response_html .= '<p>Please enter your name</p>'; 
      $this->response_status = 0; 
     } 

     // Check email 
     if(!$this->email) 
     { 
      $this->response_html .= '<p>Please enter an e-mail address</p>'; 
      $this->response_status = 0; 
     } 

     // Check valid email 
     if($this->email && !$this->validateEmail()) 
     { 
      $this->response_html .= '<p>Please enter a valid e-mail address</p>'; 
      $this->response_status = 0; 
     } 

     // Check message length 
     if(!$this->message || strlen($this->message) < $this->message_min_length) 
     { 
      $this->response_html .= '<p>Please enter your message. It should have at least '.$this->message_min_length.' characters</p>'; 
      $this->response_status = 0; 
     } 
    } 


    private function sendEmail(){ 
     $mail = mail($this->email_admin, $this->subject, $this->message, 
      "From: ".$this->name." <".$this->email.">\r\n" 
      ."Reply-To: ".$this->email."\r\n" 
     ."X-Mailer: PHP/" . phpversion()); 

     if($mail) 
     { 
      $this->response_status = 1; 
      $this->response_html = '<p>Thank You!</p>'; 
     } 
    } 


    function sendRequest(){ 
     $this->validateFields(); 
     if($this->response_status) 
     { 
      $this->sendEmail(); 
     } 

     $response = array(); 
     $response['status'] = $this->response_status; 
     $response['html'] = $this->response_html; 

     echo json_encode($response); 
    } 
} 


$contact_form = new Contact_Form($_POST, $admin_email, $message_min_length); 
$contact_form->sendRequest(); 

?> 
+0

任何地方出错? – Brewal

+0

“不行”对我们或你没有多大用处。什么不工作?你有什么错误,如果有的话?你期望发生什么,但实际发生了什么?你有什么试图解决它? – Styphon

+0

http://stackoverflow.com/questions/2896280/debugging-php-mail-and-or-phpmailer使用PHPMailer。这是更有前途,更容易调试 – Jebin

回答

0

您需要添加的方法,默认将使用GET

method="POST" 

在这里

<form id="contact-form" class="contact-form" action="_include/php/contact.php"> 

最终结果将是

<form id="contact-form" method="POST" class="contact-form" action="_include/php/contact.php"> 

确保你的行动指向正确的文件夹为好。

+0

感谢您的帮助。我已经检查了操作并将其指向了正确的文件夹。我也添加了该方法,但仍不发送电子邮件。它什么都不做:( – user3469485