Hi,

I have a form that allows the uploading of an mp3 file to a directory on the server. Which works fine.

Then all the form contents are emailed, including the attached mp3.

Issue is the mp3 is opening in the body of the email (not attached) and is in text format.

I'm using the below code:

function mail_attachment ($from , $to, $subject, $message, $attachment,$direct){   
$fileatt_type = "audio/mpeg"; // File Type (also tried "application/octet-stream")
$files=$attachment;
$email_from = $from; // from email address 
$email_subject =  $subject; // Subject of the email 
$email_txt = $message; // Email Message
$email_to = $to; // to email address
$headers = "From: ".$email_from;
$msg_txt="\n\n";
$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}\""; 
$email_txt .= $msg_txt;
$email_message .= "This is a multi-part message in MIME format.\n\n" . 
"--{$mime_boundary}\n" . 
"Content-Type:text/html; charset=\"utf-8\"\n" . 
"Content-Transfer-Encoding: 7bit\n\n" . 
$email_txt . "\n\n"; 
$email_message .= "--{$mime_boundary}\n";
for($x=0;$x<count($files);$x++)
{
#
$file = fopen($direct."/".$files[$x],"rb");
#
$data = fread($file,filesize($direct."/".$files[$x]));
#
fclose($file);
#
$data = chunk_split(base64_encode($data));
#  
$email_message .= "--{$mime_boundary}\n";
$email_message .= "Content-Type: {$fileatt_type};\n" . 
" name=\"{$files[$x]}\"\n" . 
"Content-Transfer-Encoding: base64\n\n" . 
$data . "\n\n" ; 
#
}
$email_message .= "--{$mime_boundary}--\n";     
$s=mail($email_to, $email_subject, $email_message, $headers); 
return $s;
}

Would greatly appreciate any help.

Cheers

Recommended Answers

All 3 Replies

function mail_attachment ($from , $to, $subject, $message, $attachment,$direct){   
$fileatt_type = "audio/mpeg"; // File Type (also tried "application/octet-stream")
$files=$attachment;
$email_from = $from; // from email address 
$email_subject =  $subject; // Subject of the email 
$email_txt = $message; // Email Message
$email_to = $to; // to email address
$headers = "From: ".$email_from;
$msg_txt="\n\n";
$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}\""; 
$email_txt .= $msg_txt;
$email_message .= "This is a multi-part message in MIME format.\n\n" . 
"--{$mime_boundary}\n" . 
"Content-Type:text/html; charset=\"utf-8\"\n" . 
"Content-Transfer-Encoding: 7bit\n\n" . 
$email_txt . "\n\n"; 
$email_message .= "--{$mime_boundary}\n";
for($x=0;$x<count($files);$x++)
{
#
$file = fopen($direct."/".$files[$x],"rb");
#
$data = fread($file,filesize($direct."/".$files[$x]));
#
fclose($file);
#
$data = chunk_split(base64_encode($data));
#  
$email_message .= "--{$mime_boundary}\n";
$email_message .= "Content-Type: {$fileatt_type};\n" . 
" name=\"{$files[$x]}\"\n" . "Content-Disposition: attachment;\n". 
"Content-Transfer-Encoding: base64\n\n" . 
$data . "\n\n" ; 
#
}
$email_message .= "--{$mime_boundary}--\n";     
$s=mail($email_to, $email_subject, $email_message, $headers); 
return $s;

the only alteration I made is line 35,
havent checked the code this portable hasnt access to a php localserver

Hi & thanks for replying,

Tried your suggestion but mp3 remained in text format & not attached.
This is driving me crazy!

I may have messed up the format of the content-disposition link,
have not access to the library of code scraps usually use, (holidays) google for mime file handling content-type content-disposition, there may be a specific order that filetyp disposition and name have to be in for it to work

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.