如何使用PHP发送pdf附件

时间:2015-08-24 03:09:23

标签: php

美好的一天!如果格式是在msword中,但是当我将其更改为PDF时,我的代码工作正常,它会被破坏我该怎么办?请帮帮我。

    $headers = "From:<noreply@example.com.ph>"; 
    $to = 'example@example.com'; 
    $subject = 'Purchase Order'; 

    $message .= 'Please see attached file';




        $txt .=" <html> <body> 



    <p><b> PO Number:</b> 
    $purchasenumber</p>  
    <p><b> Style Code:</b> $styleCode</p>  
    <p><b> Generic Number:</b> $gennum</p>  
    <p><b> Vendor Name:</b> $vendname</p>  
    <p><b> Planned Delivery Date:</b> 
    $pdelivdate</p> <br/> <br/>


          <table border=1 style='width:100%' cellpadding='0'> 
                       <thead>
                             <tr>
                                <th width='16.7%'>Material Number</th>
                                <th width='16.7%'>Color</th>
                                <th width='16.7%'>Size</th>
                                <th width='16.7%'>Ordered QTY</th>                                        
                                <th width='16.7%'>Total Cost</th>
                                <th width='16.7%'>Total SRP</th>                                          
                             </tr>
                        </thead>  

                         <tbody>



        ";

        $statement = $db->prepare("SELECT * FROM purchaseorderproductitem where purchaseorderID = :pid");
             $statement->execute(array(':pid' => $purchasenumber));


             foreach ($statement->fetchAll() as $row)
             {   $matnum = $row['materialnumber']; $color = $row['color']; $size = $row['size']; $qty = $row['quantity']; $units = $row['units']; $curcost = $qty * $cost;  $cursrp = $qty * $srp;   $curcost = number_format($curcost, 2, '.', ''); $cursrp = number_format($cursrp, 2, '.', '');

          $txt .="

        <tr> <td width='16.7%'>$matnum</td> <td width='16.7%'>$color</td> <td width='16.7%'>$size</td> <td width='16.7%'>$qty $units</td> <td width='16.7%'>$curcost</td> <td width='16.7%'>$cursrp</td> </tr>


        ";


            }
                 $txt .="

        <tr> <td width='16.7%' text-align:'center'>Total</td> <td width='16.7%'>&nbsp;</td> <td width='16.7%'>&nbsp;</td> <td width='16.7%'>$totalqty pcs</td> <td width='16.7%'>$totalcost</td> <td width='16.7%'>$totalsrp  </td> </tr>

        </body> </table> </html>
             "; 


     // Always set content-type when sending HTML email $message = "MIME-Version: 1.0" . "\r\n"; // $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; $message .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";


 $fileatt_name2 = "PurchaseOrder.pdf";

 $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

  // Add the headers for a file attachment $headers .= "\nMIME-Version:
    1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; $data2 = chunk_split(base64_encode($txt));


$message = "{$mime_boundary}\n" . "Content-Type: text/plain; charset=iso-8859-1; format=flowed\n" . "Content-Transfer-Encoding: 7bit\n\n" .

$message .= "{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" .

 // Add file attachment to the message $message .= "--{$mime_boundary}\n" . "Content-Type: application/octet-stream;\n" . // {$fileatt_type} " name=\"{$fileatt_name2}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileatt_name2}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data2 . "\n\n" . "--{$mime_boundary}--\n";


// Send the message $send = mail($to, $subject, $message, $headers);

你能帮我解决一下这个问题吗?提前谢谢!

1 个答案:

答案 0 :(得分:2)

您可以结合使用PHPMailer:

http://phpmailer.worxware.com/

和TCPDF:

http://www.tcpdf.org/

完成此任务。我不会详细介绍这些过程,因为代码示例创建起来非常繁琐但是这两个软件都有详细的文档和示例:

https://github.com/Synchro/PHPMailer

在这里:

http://www.tcpdf.org/docs.php

编辑:

如果你不想使用像PHPMailer那样的东西,那么我会确保发送正确的标题。

我在这里找到的一个有用的技巧是,如果你在文本编辑器中打开所述损坏的文件,你通常会在一开始就找到有关处理输出时可能发生的任何错误的有用信息。

编辑:

在这里猜测,但我认为你的最后几行代码应该如下所示:

// Add the headers for a file attachment 
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . "boundary=\"{$mime_boundary}\""; $data2 = chunk_split(base64_encode($txt));
$headers .= "{$mime_boundary}\n"."Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n";
// Add file attachment to the message 
$headers .= "--{$mime_boundary}\n" . "Content-Type: application/octet-stream;\n" . {$fileatt_type} " name=\"{$fileatt_name2}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileatt_name2}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data2 . "\n\n" . "--{$mime_boundary}--\n";
// Send the message 
$send = mail($to, $subject, $message, $headers);

您已将其中一个语句的结尾连接起来,而不是以分号结尾;

您是将标题添加到$ message变量中,它们应该更好地在$ headers变量中吗?

您已添加其中一个标题两次。