2012-11-28 113 views
-1

我有一個表格。用下面的:PHP附件聯繫表格

HTML:

<form name="feedback_form" method="post" enctype="multipart/form-data" action="" class="feedback_form"> 
    <input type="text" name="field-name" value="İsim" title="İsim" class="field-name form_field"> 
    <input type="text" name="field-email" value="Email" title="Email" class="field-email form_field"> 
    <input type="text" name="field-subject" value="Başlık" title="Başlık" class="field-subject form_field"> 
    <textarea name="field-message" cols="45" rows="5" title="Mesajınız..." class="field-message form_field">Mesajınız...</textarea> 
    <label for='uploaded_file'>Fotoğrafınızı Yükleyin:</label> 
    <input type="file" name="field-file" value="File"> 
    <br> 
    <input type="reset" name="reset" id="reset2" value="Temizle" class="feedback_reset"> 
    <input type="button" name="submit" class="feedback_go" id="submit2" value="Gönder">          
</form> 

PHP:

<?php 
header('Content-Type: text/html; charset=utf-8'); 

function sendFeedback($feedback_email, $feedback_msg, $feedback_name, $feedback_subject, $feedback_file) { 


    /* EDIT THIS */ 
    $admin_email = "[email protected]"; 
    if ($feedback_subject == "Subject" || empty($feedback_subject)) { 
     $subj = "Email from your site"; 
    } else { 
     $subj = $feedback_subject; 
    } 


    /* //EDIT THIS */ 


    $message = " 
    <html> 
    <head> 
     <title>Websitenizin emaili</title> 
    </head> 
    <body> 
     <p><a href='mailto:".$feedback_email."'>".$feedback_name."</a> send this message:</p> 
     <p>".$feedback_msg."</p> 
     <p>".$subject."</p> 
    </body> 
    </html> 
    "; 
    $headers = 'MIME-Version: 1.0' . "\r\n"; 
    $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n"; 

    if ($feedback_name!=="Name" && $feedback_email!=="Email" && !empty($feedback_email) && !empty($feedback_msg) && !empty($feedback_name)) { 
     if ($feedback_email == "mail_error") { 
      echo "<span class='ajaxok'>Geçersiz email adresi.</span>"; 
     } else {    
      mail($admin_email, $subj, $message, $headers); 
      echo "<span class='ajaxok'>Teşekkürler. Mesajınız gönderilmiştir.</span>"; 
     } 
    } else { 
     echo "<span class='ajaxalert'>Lütfen zorunlu alanları doldurunuz.</span>";  
    } 


} 

sendFeedback($_POST['email'], $_POST['message'], $_POST['name'], $_POST['subject'], $_FILES['file']); 



?> 

當這種形式發送郵件,發送郵件,工作。但只有主題,消息,姓名和電子郵件。我想在這個php代碼中添加圖片上傳。但我不知道我該怎麼辦?請幫幫我。

回答

0

看看這裏:http://pastebin.com/nAErtHgt

我寫這篇文章的Mail is sent without the attachment但似乎無法使用它。

添加此功能,您的PHP文件

function addattachment($file){ 
     $fname = substr(strrchr($file, "/"), 1); 
     $data = file_get_contents($file); 
     $i = count($this->parts); 
     $content_id = "part$i." . sprintf("%09d", crc32($fname)) . strrchr($this->to_address, "@"); 
     $this->parts[$i] = "Content-Type: ".mime_content_type($file)."; name=\"$fname\"\r\n" . 
          "Content-Transfer-Encoding: base64\r\n" . 
          "Content-ID: <$content_id>\r\n" . 
          "Content-Disposition: inline;\n" . 
          " filename=\"$fname\"\r\n" . 
          "\n" . 
          chunk_split(base64_encode($data), 68, "\n"); 
     return $content_id; 
    } 

和$消息,$頭

之間添加

$message .= addattachment($feedback_file); 
並應工作。

讓我知道是否爲你工作。

0

我不推薦使用PHP的郵件功能,使用類似PHPMailer的,相反,它是與多發送郵件合理的類,附件等

http://sourceforge.net/projects/phpmailer/(應該是很多替代這個的,但它適用於我)

然後只需將上傳的tmpfile添加爲附件即可。