duan2428 2015-08-24 03:09
浏览 7

如何使用PHP发送pdf附件[复制]

This question already has an answer here:

Good day! My code is working great if the format is in msword but when i changed it to PDF it becomes corrupted what should i do? Please help me.

    $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" . "
"; // $headers .= "Content-type:text/html;charset=UTF-8" . "
"; $message .= 'Content-type: text/html; charset=iso-8859-1' . "
";


 $fileatt_name2 = "PurchaseOrder.pdf";

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

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


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

" .

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

" .

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

" . $data2 . "

" . "--{$mime_boundary}--
";


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

Can you please help me to solve this issue? Thanks in advance!

</div>
  • 写回答

1条回答 默认 最新

  • doudun6928 2015-08-24 03:15
    关注

    You can use a combination of PHPMailer:

    http://phpmailer.worxware.com/

    And TCPDF:

    http://www.tcpdf.org/

    To accomplish this task. I will not cover the processes in detail as code examples would be quite tedious to create however both pieces of software have detailed documentation and examples found here:

    https://github.com/Synchro/PHPMailer

    And here:

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

    Edit:

    If you do not want to to use something that just works like PHPMailer then I would ensure that the correct headers are being sent.

    One useful trick i have found here too is that if you open said corrupted file in a text editor, you most usually find useful information at the very beginning relating to any errors that may have occurred while processing the output.

    Edit:

    Just guessing here but your last few lines of code I believe should read as follows:

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

    You had concatenated the end of one of the statements instead of ending it with a semicolon ;

    You were adding the headers to the $message variable, they should rather be in the $headers variable correct?

    You had added one of the headers twice.

    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算