Hello,
I have the following code in a php script to send a pdf file as an attachment. It all seems to work fine, and the recipient receives the email with the pdf attached, the file size is about the right size, but when you open the pdf, it is completely blank. Any ideas why this would be?
<?php
function mail_attached($to, $from, $subject, $message, $filename, $headers = '')
{
$unique_sep = md5(uniqid(time()));
$headers .= "From: $from\n";
$headers .= "MIME-Version: 1.0\nContent-Type:"
." multipart/mixed;boundary=\"$unique_sep\";\n";
$headers .= "charset=\"iso-8859-1\"\nContent-Transfer-Encoding:"
."7bit\n\n";
$headers .= "If you are reading this, then your e-mail client does"
."not support MIME.\n";
$headers .= "--$unique_sep\n";
if(is_array($filename))
{
foreach($filename as $val)
{
if(file_exists($val['file']))
{
$headers .= "--$unique_sep\n";
$headers .= "Content-Type: {$val['mimetype']}; ".
"name=\"{$val['filename']}\"\n";
$headers .= "Content-Type: {$val['mimetype']}; ".
"name=\"{$val['filename']}\"\n";
$headers .= "Content-Transfer-Encoding: ".
"base64\n";
$headers .= "Content-Disposition: attachment;filename=\"{$val['filename']}\"\n\n";
$filedata = implode(file($val['file']), '');
$headers .= chunk_split(base64_encode($filedata));
}
else
{
echo "Error: File doesn't exist.<BR>";
}
}
}
else
{
echo "Error: Invalid parameter passed.<BR>";
}
$headers .= "--$unique_sep--\n";
if(!mail($to, $subject, $message, $headers))
{
echo "Error: mail() function failed!<BR>";
}
echo "Error: mail() function failed!<BR>";
}
} / End of mail_attached() function /
$file_array = array(0=>array('file'=>'/tmp/xxx.pdf','mimetype'=>'application/octet-stream','filename'=>'something.pdf'));
mail_attached("me@myaddress","me@myotheraddress","something","hello you goose",$file_array,"");
?>