It may be not having a -- at the end of the last boundary tag
ie $email_message .= "--{$mime_boundary}\n";
to $email_message .= "--{$mime_boundary}--\n";
I'm not sure it there is a difference between multipart/mixed and multipart/alternative but this works for me
# Setup mime boundary
$mime_boundary = 'Multipart_Boundary_x'.md5(time()).'x';
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\n";
$headers .= "Content-Transfer-Encoding: 7bit\n";
$body = "This is a multi-part message in mime format.\n\n";
# Add in plain text version
$body .= "--$mime_boundary\n";
$body .= "Content-Type: text/plain; charset=\"charset=us-ascii\"\n";
$body .= "Content-Transfer-Encoding: 7bit\n\n";
$body .= $text_content;
$body .= "\n\n";
# Add in HTML version
$body .= "--$mime_boundary\n";
$body .= "Content-Type: text/html; charset=\"UTF-8\"\n";
$body .= "Content-Transfer-Encoding: 7bit\n\n";
$body .= $html_content;
$body .= "\n\n";
#Attachments
if ($path!='' && $filename!=''){
$file_size = filesize($path.$filename);
$handle = fopen($path.$filename, "r");
$filecontent = fread($handle, $file_size);
fclose($handle);
$filecontent = chunk_split(base64_encode($filecontent));
$body .= "--$mime_boundary\n";
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$body .= $filecontent."\r\n\r\n";
}
# End email
$body .= "--$mime_boundary--\n";