2013-02-24 58 views
0

我有一個問題,我想通過電子郵件發送一個文件(Photoshop,.gif等)文件與PHP。 我將數據從HTML文檔發佈到以下PHP文件。 問題:我收到電子郵件但文件已損壞。 任何想法,爲什麼這不起作用?通過郵件傳遞文件()PHP

$to = '[email protected]'; 
    $subject = 'Order'; 

    $name = strip_tags($_POST['name']); 
    $email = strip_tags($_POST['email']); 
    $plz = strip_tags($_POST['plz']); 
    $city = strip_tags($_POST['city']); 
    $street = strip_tags($_POST['street']); 
    $nr = strip_tags($_POST['nr']); 

    $plzdelivery = strip_tags($_POST['plz-delivery']); 
    $citydelivery = strip_tags($_POST['city-delivery']); 
    $streetdelivery = strip_tags($_POST['street-delivery']); 
    $nrdelivery = strip_tags($_POST['nr-delivery']); 

    $buttonsize = strip_tags($_POST['button-size']); 
    $count = strip_tags($_POST['count']); 

    $message = "Name: ".$name." Email: ".$email." Plz: ".$plz." Stadt: ".$city." Strasse: ".$street." Hnr: ".$nr." Button: ".$buttonsize." Anzahl: ".$count; 

    $type = $_FILES['file']['type']; 
    $attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name']))); 
    $filename = $_FILES['file']['name']; 

    $boundary =md5(date('r', time())); 

    $headers = "From: [email protected]\r\nReply-To: [email protected]"; 
    $headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\""; 

    $message="This is a multi-part message in MIME format. 
--_1_$boundary 
Content-Type: multipart/alternative; boundary=\"_2_$boundary\" 

--_2_$boundary 
Content-Type: text/plain; charset=\"iso-8859-1\" 
Content-Transfer-Encoding: 7bit 

$message 

--_2_$boundary-- 
--_1_$boundary 
Content-Type: application/octet-stream; name=\"$filename\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

$attachment 
--_1_$boundary--"; 

    mail($to, $subject, $message, $headers); 
+1

如果你堅持使用這種繁瑣的代碼發送郵件,那麼請自己搞清楚。我們不在這裏修補互聯網上的隨機郵件/ MIME工藝代碼片段。其他人都可以使用PHPMailer或SwiftMailer,它有內置的支持。 – mario 2013-02-24 20:41:42

+2

'mail()'是無用的垃圾。如果可能的話,千萬不要使用它。 – 2013-02-24 20:44:30

+0

所以你只發送字符串,而不是文件。你可以在這裏閱讀關於發送郵件通過PHP http://webcheatsheet.com/PHP/send_email_text_html_attachment.php – Eugen 2013-02-24 20:48:46

回答

0

簡短的回答:想都別想。

PHP的mail()功能非常功能上的缺點。如果你甚至正在考慮嘗試使用它發送附件,你會感到很多挫折和浪費時間。

我可以給你的最佳答案是使用第三方庫。有幾個好的可用。爲什麼要浪費你數週的時間寫出一些不夠好的東西,而其他人已經花了好幾年的時間爲你做出了正確的選擇。

我的建議是phpMailer,但也有其他幾種。