Hi,
I have written a php script to mail a jpg file as an attachment. It mails the message an attachment but the attachment comes as a file.bin file , not as an image file. I cant even open this file in an image viewer. Plz tell me what is wrong in my coding. My coding is here,
<?php
//This php program will mail an attachment with a text message.
$headers = "From:chamalsl@yahoo.com";
$message ="Please check the attachment";
//Read the file
$file = fopen("bruce.jpg",'rb');
$data = fread($file,filesize("bruce.jpg"));
fclose($file);
//Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n".
"Content-Type: multipart/mixed;\n".
" boundary=\"{$mime_boundary}\"";
//Add the text message
$message = "This is a multipart message in MIME format.\n\n".
"--{$mime_boundary}\n".
"Content-Type: text/plain; charset=\"iso-8859-1\"\n".
"Content-Transfer-Encoding: 7bit\n\n".
$message."\n\n";
//Base64 encode the file data
$data = chunk_split(base64_encode($data));
//Add file attachment to the message
$message .= "--{$mime_boundary}\n".
"Content-Type:image/jpeg;\n".
"name =\"bruce.jpg\"\n".
"Content_Disposition: attachment;\n".
"filename=\"bruce.jpg\"\n".
"Content-Transfer-Encoding: base64\n\n".
$data."\n\n".
"--{$mime_boundary}--\n";
//Email the message
$sent = @mail("chamalsl@yahoo.com","Sinhala Email",$message,$headers);
if($sent)
{
echo "Mail is sent";
}
else
{
echo "Mail could not be sent";
}
?>
Thanks,
Chamal.