php
2015-08-24 54 views 0 likes 
0

美好的一天!我的代碼工作得很好,如果格式是在msword中,但是當我將其更改爲PDF時,它會變成損壞,我該怎麼辦?請幫幫我。如何使用PHP發送pdf附件

$headers = "From:<[email protected]>"; 
    $to = '[email protected]'; 
    $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); 

你能幫我解決這個問題嗎?提前致謝!

回答

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變量中正確嗎?

您已經添加了一個標頭兩次。

+0

你能解釋一下嗎?其實我無法使用任何phpmailer和tcpdf這就是爲什麼我問。對不起 – Somi

+0

我非常隱約地記得,我曾經在一個時間點下載了損壞的pdf文件。按照我在文本編輯器中打開文件所說的那樣,它可能會顯示您理解的錯誤。問題是沒有發送正確的標題。當你說你不能使用phpmailer/tcpdf時,問題是什麼?他們都是堅如磐石的。 –

+0

我的意思是我不知道如何使用它們。這是我第一次在電子郵件中附加文件。簡而言之,新手。對不起 – Somi

相關問題