doujiang2641 2013-02-24 20:19
浏览 16
已采纳

通过mail()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
Reply-To: webmaster@example.com";
    $headers .= "
MIME-Version: 1.0
Content-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);
  • 写回答

2条回答 默认 最新

  • doubishi8303 2013-02-24 20:56
    关注

    Short answer: Don't even try.

    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.

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题