Corrupted PDF email attachment using PHP mail script

Hi all,

I am hoping to find an experienced PHP developer to help me troubleshoot an attachment issue with my email script. The user enters his/her email address as part of a form input and this script is supposed to attach a remotely-hosted file (PDF or ZIP) along with some body text. The same script works great for the ZIP file attachment but NOT for PDF. The script does deliver the PDF and the file properly shows up in the email as an attachment – the problem is when the user attempts to ‘open’ the PDF. The Adobe Acrobat Reader populates an error stating that the file is corrupted – which could be do to an encoding issue while sent as an attachment. However, I’m using base64_encoding and I’m using ‘application/pdf’ as the file type. I admit I’m a bit of a beginner at discerning PHP code – so I hope that if the community here takes a look at my code line by line – they can advise me where I am going wrong. I have been doing all kinds of research on this for the past week – and nothing I’ve changed has seemed to make a difference. Any help would be greatly appreciated. I’m going to try and include the code below:

<?php
$file_path = "http://www.mitchellmetals.net/images/drawings_walkway/w1.pdf"; // server path where file is placed
$file_path_type = "application/pdf"; // File Type
$file_path_name = "w1.pdf"; // this file name will be used at reciever end
$headers .= 'Content-type: text/html; charset=utf-8' . "\\r\
";
$from = "noreply@mitchellmetals.net"; // E-mail address of sender
$to = $_POST['email']; // E-mail address of reciever
$subject = "Drawing from Mitchell Metals"; // Subject of email
$message = "Thank you for downloading the <b>Walkway Canopy with Beams PDF file</b> from mitchellmetals.net. For a free estimate or budget number on a canopy, please email us at <a href='mailto:sales@mitchellmetals.net'>sales@mitchellmetals.net</a>. Please call 770-431-7300 if Mitchell Metals can assist you with your next project.";

$headers = "From: ".$from;

$file = fopen($file_path,'rb');
$data = fread($file,filesize($file_path));
fclose($file);

$rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$rand}x";

$headers .= "\
MIME-Version: 1.0\
" .
"Content-Type: multipart/mixed;\
" .
" boundary=\\"{$mime_boundary}\\"";

$message .= "This is a multi-part message in MIME format.\
\
" .
"--{$mime_boundary}\
" .
"Content-Type:text/html; charset=\\"iso-8859-1\\"\
" .
"Content-Transfer-Encoding: 7bit\
\
" .
$message .= "\
\
";


$data = chunk_split(base64_encode($data));

$message .= "--{$mime_boundary}\
" .
"Content-Type: {$file_path_type};\
" .
" name=\\"{$file_path_name}\\"\
" .
"Content-Disposition: attachment;\
" .
" filename=\\"{$file_path_name}\\"\
" .
"Content-Transfer-Encoding: base64\
" .
$data .= "\
\
" .
"--{$mime_boundary}--\
";

if(@mail($to, $subject, $message, $headers)) {
echo "File sent!";

} else {
echo 'Failed';
}
?>

Welcome to Sitepoint Forums, @mediumblue;
There are innumerable PHP experts here and I can guarantee someone will be able to help you with this issue.

After solving your problem, please be sure to continue to browse the vast collection of useful knowledge and information we have here and add your knowledge and experience.

At all times, though, be aware of our Rules of Conduct.

I think it may be the Content-Transfer-Encoding header.
Try to remove it and only use Content-Type: application/pdf
Or try to have it on 8 bits
Source

However, it is a good idea to use PHPMailer for this, it’s easy and safe.
I will give it a try latter and check some real code.