2014-02-10 99 views
1

我一直在試圖自定義HTML網站模板附帶的聯繫表單來添加文件附件/上傳但沒有成功。試圖做到這一點而不是嵌入不同聯繫人表單的原因很簡單,因爲它的響應速度與網站模板的其他部分保持一致。任何幫助都會很棒。如何將文件附件添加到現有的聯繫人表單?

這是聯繫表單的HTML部分:

<div class="row"> 
    <div class="span9"> 

     <form id="contact-form" class="contact-form" action="#"> 
      <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> 

,這裏是PHP的一部分:

<?php 

header('Cache-Control: no-cache, must-revalidate'); 
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); 
header('Content-type: application/json'); 

$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(); 

?> 

我已經嘗試了許多不同的形式,但所有我必須嚴重改變表單的CSS/HTML格式,導致網站模板一路崩潰。

+0

附加文件在PHP郵件
下載PHP簡單郵件腳本最簡單的方式,使用PHPMailer的。 –

回答

0

閱讀此頁http://webcheatsheet.com/php/send_email_text_html_attachment.php
添加<input type="file" >爲HTML和閱讀上面鏈接,在Github上 https://github.com/eoghanobrien/php-simple-mail

+0

我無法啓動它。沒有附件的電子郵件工作得很好,但我無法讓它發送實際的附件。你能解釋一下嗎? –

+0

下載'https:// github.com/eoghanobrien/php-simple-mail'並獲取簡單附件 –

+0

這是可行的。請接受我的答案...;) –

相關問題