美好的一天!我的代碼工作得很好,如果格式是在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%'> </td> <td width='16.7%'> </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);
你能幫我解決這個問題嗎?提前致謝!
你能解釋一下嗎?其實我無法使用任何phpmailer和tcpdf這就是爲什麼我問。對不起 – Somi
我非常隱約地記得,我曾經在一個時間點下載了損壞的pdf文件。按照我在文本編輯器中打開文件所說的那樣,它可能會顯示您理解的錯誤。問題是沒有發送正確的標題。當你說你不能使用phpmailer/tcpdf時,問題是什麼?他們都是堅如磐石的。 –
我的意思是我不知道如何使用它們。這是我第一次在電子郵件中附加文件。簡而言之,新手。對不起 – Somi