K
K
Kirill Penzin2015-02-04 15:52:42
PHP
Kirill Penzin, 2015-02-04 15:52:42

Why does a broken pdf-file arrive when sent using php?

Hello, I've been working on a problem for half a day. There is a source pdf-file on my site, it must be sent attached to the letter. The file finds, opens, reads, but when trying to open the resulting file, adobe reader swears that the file is damaged or has not been decoded, etc.
I checked the file from the site - clean, working. Tell me what am I doing wrong?
PS chunk_split () set, just in this version I already try without it. No difference. Source file 56 kb, received - 80 (78 without chunk) kb

<?php 
$to  = "[email protected]" ;  

$subject = "Подтверждение заказа"; 

//$message = file_get_contents('../files/accept_'.$_POST['id'].'.php');


$file=fopen('../files/orderpdf_'.$_POST['id'].'.pdf','rb');
$name='../files/orderpdf_'.$_POST['id'].'.pdf';
$attachment=fread($file, filesize($name));
fclose($file);
$attachment=base64_encode($attachment);


unlink('../files/orderpdf_'.$_POST['id'].'.pdf');



$headers  = "MIME-Version: 1.0 \r\n";
$headers .= "Content-Type: multipart/mixed; boundary=pdfnik \r\n";
$headers .= "charset=utf-8 \r\n";
$file= basename('../files/orderpdf_'.$_POST['id'].'.pdf');


$body  = "--pdfnik\n";   
$body .= "Content-Type: application/octet-stream; ";
$body .= "name=".basename('../files/orderpdf_'.$_POST['id'].'.pdf')."\n";
$body .= "Content-Transfer-Encoding:base64n\n";
$body .= "Content-Disposition:attachment; filename=\"$file\"\n\n";
$body .= $attachment;
$body .= "pdfnik--\n";

mail($to, $subject, $body, $headers); 
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Max, 2015-02-04
@kir_vesp

There's a typo at the end, it should be like this
body .= "Content-Transfer-Encoding:base64\n\n";

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question