This question already has an answer here:
- php send e-mail with PDF attachment 4 answers
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%'> </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" . "
"; // $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>