0

私の現在のプロジェクトでは、その場で PDF ファイルを作成し、メールの添付ファイルとして添付して送信する必要があります。それは正常に動作し、ファイルが生成され、私が提供した電子メールに送信されます。Microsoft Outlook または Windows Live アカウントに送信すると、PDF が添付されていますが、pdf でファイルを開くことができず、破損しているというエラーが表示されます。しかし、Gmail と Yahoo では問題なく動作します。誰もこれに対する解決策を持っていますか。以下は私のコードです

$dompdf = new DOMPDF();
    $dompdf->load_html($message);
    $dompdf->set_paper("a4", "landscape");
    $dompdf->render();

    // The next call will store the entire PDF as a string in $pdf
    $pdf = $dompdf->output();

    // You can now write $pdf to disk, store it in a database or stream it to the client.
    file_put_contents("pdfs/invoice.pdf", $pdf);


    $fileatt = "pdfs/invoice.pdf"; // Path to the file
    $fileatt_type = "pdf"; // File Type
    $fileatt_name = "invoice.pdf"; // Filename that will be used for the file as the attachment


    $fp = fopen($fileatt, "rb");
    $file = fread($fp, filesize($fileatt));

    $file = chunk_split(base64_encode($file));
    $num = md5(time());




    $to = "mail@mail.com";

    $subject = "Invoice";
    $headers = "From: " . "Manager" . "<" . "mail@mail.com" . ">\r\n";
    $headers  .= "MIME-Version: 1.0\r\n";
    $headers  .= "Content-Type: multipart/mixed; ";
    $headers  .= "boundary=".$num."\r\n";
    $headers  .= "--$num\r\n";

    $headers .= "Message-ID: <" . gettimeofday() . " TheSystem@" . $_SERVER['SERVER_NAME'] . ">\r\n";
    $headers .= "X-Mailer: PHP v" . phpversion() . "\r\n";



    $headers  .= "Content-Type:".$fileatt_type." ";
    $headers  .= "name=\"".$fileatt_name."\"r\n";
    $headers  .= "Content-Transfer-Encoding: base64\r\n";
    $headers  .= "Content-Disposition: attachment; ";
    $headers  .= "filename=\"".$fileatt_name."\"\r\n";
    $headers  .= "".$file."\r\n";
    $headers  .= "--".$num."\r\n";

    $headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
    $headers .= "".$message."\r\n";
    $headers .= "--".$num."--";

    if (mail($to, $subject, $message, $headers)) {
        fclose($fp);
        echo "Success";
        //header("location: client.php?m=1");
    } else {
        echo "Error";
        //header("location: client.php?m=0");
    }

誰かが問題を解決するのを手伝ってくれることを願っています。

4

2 に答える 2

2

独自の MIME メッセージを作成することは、決して良い考えではありません。代わりにPHPMailerまたはSwiftmailerを使用してください。どちらも、添付ファイルを含むメッセージの構築という重労働を処理します。何よりも、どちらも無料であり、組み込みの PHP メール機能よりもはるかに優れた機能を発揮します。上記のコードは、どちらのパッケージでも、メール送信コードの 6 行のうち約 5 行に減らすことができます。

于 2011-04-11T14:29:21.593 に答える
1

私はほぼ 1 日これに苦労しましたが、境界の先頭にタブ文字 ( \t) を付けないと、Outlook が添付ファイルを認識しないように見えることがわかりました。

例えば ​​:

$headers.="Content-type: multipart/mixed;\r\n\tboundary=\"uniqueID\"\r\n\r\n";

また、両方\r\nを一緒に使用し
、各境界 (およびその指示) とそのコンテンツの間に明確な線があることを確認する必要があります。

于 2012-02-01T15:57:40.690 に答える