0

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" . "\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);

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

Somi
  • 21
  • 7

1 Answers1

2

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 .= "\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);

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.

Craig van Tonder
  • 7,497
  • 18
  • 64
  • 109
  • can you explain more? Actually i wasn't able to use any of phpmailer and tcpdf that's why i'm asking. Sorry – Somi Aug 24 '15 at 03:23
  • I recall very vaguely that i had issues with corrupted pdf files being downloaded at one point in time. Do what i said with opening the file in a text editor aswell, it might show errors that you understand. The issue was that the correct headers were not being sent. When you say that you cannot use phpmailer/tcpdf what was the issue? They are both rock solid. – Craig van Tonder Aug 24 '15 at 03:25
  • I mean i don't have any idea on how to use them. Its my first time in attaching file in email. Newbie in short. Sorry – Somi Aug 24 '15 at 03:27
  • No worries friend, tell me... The file that gets generated, how big is it? – Craig van Tonder Aug 24 '15 at 03:28
  • Its 3KB. But its corrupted in my codes but when i changed the pdf to doc it works fine. I don't get it. – Somi Aug 24 '15 at 03:31
  • the last two lines in your code, why is it commented? Is it like that in the source or was it an error when copying it into here? You setting the headers twice, then you have it again commented out then you have the sending of the email commented out too. What lol? – Craig van Tonder Aug 24 '15 at 03:32
  • NO its not i forgot to hit enter between // Send the message and $send and // Add file attachment to the message to $message. Sorry i didn't double check the format of my codes when copying here. – Somi Aug 24 '15 at 03:35
  • Okay and when you open the corrupted file in your text editor, any info? – Craig van Tonder Aug 24 '15 at 03:39
  • Adobe Reader could not open 'PurchaseOrder.pdf' because it is either not a supported file tyoe or because the has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded) - That's the exact info while opening the pdf attachment – Somi Aug 24 '15 at 03:43
  • @Somi Do not use Adobe reader to open the pdf file. Use a text editor like notepad in Windows. If you do that then it should give you errors, those errors generally tell you why things went wrong. Anyways, see my update? – Craig van Tonder Aug 24 '15 at 03:47
  • Its working fine if i use text editor and doc but not in pdf. Your updated codes does not work it gives me error. – Somi Aug 24 '15 at 03:56
  • It should not be able to open the pdf in the text editor, if it does it might have a few lines of readable text then a bunch of garbage. Any readable text? Please edit in the code as it is in the source, it seems that you have removed peices of it while copying it into the SO question. – Craig van Tonder Aug 24 '15 at 03:58
  • Do you have a sample pdf attachment that is working? Can i see it and i'll follow it by my codes – Somi Aug 24 '15 at 04:09
  • Sorry but this is approaching consulting and beyond the scope of your question. As stated, both options and others provided in the duplicate links come with detailed information and examples of how you could accomplish this and many other tasks. I cannot assist more than this as I do not know which parser to are working with to start off with, the code seems to be incomplete and its making it difficult to provide an accurate answer. – Craig van Tonder Aug 24 '15 at 04:12