通过mail()PHP传递文件 [英] Pass File via mail() PHP

查看:86
本文介绍了通过mail()PHP传递文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到问题,我想通过电子邮件使用PHP发送文件(Photoshop,.gif等)文件. 我将数据从HTML文档发布到以下PHP文件. 问题:我收到电子邮件,但文件已损坏. 知道为什么这行不通吗?

I have a Problem, I would like to send a File (Photoshop, .gif etc.) File via Email with PHP. I POST the Data from the HTML Document to the following PHP File. The Problem: I receive the Email but the File is corrupt. Any Idea why this doesn´t work??

    $to = 'admin@example.com'; 
    $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: webmaster@example.com\r\nReply-To: webmaster@example.com";
    $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);

推荐答案

简短答案:甚至不要尝试.

Short answer: Don't even try.

PHP的mail()函数在功能上非常非常短.如果您甚至正在考虑尝试使用它发送附件,那么您会感到非常沮丧和浪费时间.

PHP's mail() function is very very short on features. If you're even considering trying to send attachments using it, you are in for a lot of frustration and wasted time.

我能给您的最佳答案是使用第三方库.有几个不错的选择.为什么当别人已经花了数年时间为您做并做到正确的时候,为什么要浪费数周的时间来编写不够好的东西呢?

The best answer I can give you is to use a third party library. There are several good ones available. Why waste weeks of your time writing something that isn't going to be good enough when others have already spent years doing it for you and getting it right.

我的建议是 phpMailer ,但还有其他几个也存在.

My suggestion would be phpMailer, but several others exist as well.

这篇关于通过mail()PHP传递文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆