2013-02-24 47 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,但也有其他几种。