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]<?php
$file_path = “http://www.xxxxxx.net/1.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\n”;
$from = "[email protected]"; // E-mail address of sender
$to = $_POST[‘email’]; // E-mail address of reciever
$subject = “Drawing from xxxxxxx”; // Subject of email
$message = “Thank you for downloading the xxxremoved by modxxx.”;
$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 .= “\nMIME-Version: 1.0\n” .
“Content-Type: multipart/mixed;\n” .
" boundary="{$mime_boundary}"";
$message .= “This is a multi-part message in MIME format.\n\n” .
“–{$mime_boundary}\n” .
“Content-Type:text/html; charset=“iso-8859-1”\n” .
“Content-Transfer-Encoding: 7bit\n\n” .
$message .= “\n\n”;
$data = chunk_split(base64_encode($data));
$message .= “–{$mime_boundary}\n” .
“Content-Type: {$file_path_type};\n” .
" name="{$file_path_name}"\n" .
“Content-Disposition: attachment;\n” .
" filename="{$file_path_name}"\n" .
“Content-Transfer-Encoding: base64\n” .
$data .= “\n\n” .
“–{$mime_boundary}–\n”;
if(@mail($to, $subject, $message, $headers)) {
echo “File sent!”;
} else {
echo ‘Failed’;
}
?>[/php]