abhi10kumar 0 Junior Poster

I want to add a file which will come database that is file path. I am using the following code to send, Mail is being sent with out attachment.

$name = "Abhi";
$email = "abhi10kumar@gmail.com";
$to = "$name <$email>";
$from = "Rahul Kumar";
$subject = "Here is your attachment";
$fileatt = "./images/1.jpg";
$fileatttype = "images/jpg";
$fileattname = "1.jpg";
$headers = "From: $from";
$file = fopen($fileatt, ‘rb’);
$data = fread($file, filesize($fileatt));
fclose($file);
$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}\"";
$message = "This is a multi-part 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";
$data = chunk_split(base64_encode($data));
$message .= "–{$mime_boundary}\n" .
"Content-Type: {$fileatttype};\n" .
" name=\"{$fileattname}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileattname}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"-{$mime_boundary}-\n";
if(mail($to, $subject, $message, $headers)) 
{
echo "
The email was sent.
";
}
else 
{
echo "
There was an error sending the mail.
";
}

Problems:
1. Mail sent to spam.
2. Need send any type of files
3. Attachment is not working