0

問題があります。ファイル (Photoshop、.gif など) ファイルを PHP を使用して電子メールで送信したいと考えています。HTML ドキュメントのデータを次の PHP ファイルに POST します。問題: 電子メールを受信しましたが、ファイルが破損しています。なぜこれがうまくいかないのですか??

    $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);
4

2 に答える 2

0

簡単な答え: 試してはいけません。

PHP のmail()関数は機能が非常不足しています。それを使用して添付ファイルを送信しようと考えている場合でも、多くのフラストレーションと無駄な時間を過ごすことになります。

私ができる最善の答えは、サードパーティのライブラリを使用することです。いくつかの良いものがあります。他の人がすでに何年も費やしてあなたのためにそれを行い、それを正しくしているのに、十分に良くないものを書くのに何週間も時間を無駄にするのはなぜですか.

私の提案はphpMailerですが、他にもいくつか存在します。

于 2013-02-24T20:56:30.610 に答える
0

pear Mail_Mime パッケージをインストールすると、ファイルを簡単に添付できるようになります。

http://pear.php.net/manual/en/package.mail.mail-mime.addattachment.php

于 2013-02-24T20:51:15.370 に答える