email attachment corrupt

From: j0sh (josh_at_pixael.com)
Date: 07/30/04


Date: Fri, 30 Jul 2004 13:02:44 +0200

hi all
i'm trying to send email with attachment
email arrive but the attachment is corrupt
this is my function... but i have the same problem with class from zend
or other sites

function send_mail($to, $subject, $message, $name, $email, $file)
{
        $fp = fopen($file, "rb");
        $fcontent = fread($fp, filesize($file));
        fclose($fp);
        $data = chunk_split(base64_encode($fcontent),74,"\n");
        $data=substr($data,0,strlen($data)-1);
        $mime_boundary = "------------" . md5(uniqid(microtime()));
        $file_name = basename($file);
        $header = "From: " . $name . "<" . $email . ">\r\n";
        $header .= "MIME-Version: 1.0\r\n";
        $header .= "Content-Type: multipart/mixed;\r\n";
        $header .= " boundary=\"" . $mime_boundary . "\"\r\n";
        $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
        $content = "This is a multi-part message in MIME format.\r\n\r\n";
        $content .= "--" . $mime_boundary . "\r\n";
        $content .= "Content-Type: text/plain; charset=\"us-ascii\";
format=flowed\r\n";
        $content .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
        $content .= $message . "\r\n\r\n";
        $content .= "--" . $mime_boundary . "\r\n";
        $content .= "Content-Type: image/jpeg;\r\n name=\"$file_name\"\r\n";
        $content .= "Content-Transfer-Encoding: base64\r\n";
        $content .= "Content-Disposition: inline;\r\n filename=\"" .$file_name
. "\"\r\n\r\n";
        $content .= $data . "\r\n";
        $content .= "--" . $mime_boundary . "---";
        if (mail($to, $subject, $content, $header))
        {
                return true;
        } else {
                return false;
        }
}

some ideas?