-1

I created a codes using php its working already with attachment but its only .doc i want to change it to .pdf i tried to change the application/msword to application/pdf and the file name from PO.doc to PO.pdf but its corrupted when i receive it on my email. Can you suggest me what should i do? Below is my codes for that. Thank you.

<?php 

$headers = "From:<noreply@example.com>";
$to = 'email@example.com';
$subject = 'Purchase Order';





$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/>";



    // 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.doc";
$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/msword;\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);

?>
Somi
  • 21
  • 7
  • 3
    The file needs to be converted. You can't just change filename extensions and expect it to not have some type of corruption, we're talking about different parsing engines completely. Akin to renaming a .py file to .php, won't work. – Ohgodwhy Aug 11 '15 at 03:22
  • oh god why would you think changing a file extension magically changes the content of the file? –  Aug 11 '15 at 03:49

1 Answers1

1

This is not as simple as changing the extension (.doc, .pdf etc.) to convert a file from one type to the other. A more specialized process is needed. What you tried is analogous to taking an apple and dressing it up as a pear. It may look like a pear at first, but when taking a bite you'd discover it is actually an apple.

If you want to convert the .doc to a PDF you can convert it using programs like Microsoft Word (although I'm sure that Libre- or Open-office can do this too). If a more automated manner is desired, consider implementing other solutions like livedocs or phpdocs although the latter is quite pricy.

Depending on the platform (Linux, Windows, OSX) other options might be available as was answered in questions like this one.

Community
  • 1
  • 1