Hi,
I'm trying to create a web based way to send an email with attachment. I tried using the following code, however for pdf or xls files the files I retrieve when testing the script are broken. However for java or other text files it works just fine. It seems to be some sort of problem with mail(), as the files I retrieve are really small (less than 2Kš but $filecontent has the same size as the file I'm trying to send.
I'd reallt appreciate some comments, as I've been trying to get this working for quite some time and ran out of ideas.
Thanks a lot!
Thorsten
Below
$file is the parameter from the html form for the file upload and
$message the text the user wants to send.
At first both $headers and $msg are equal to ''
$fp=fopen($file, "rb");
$filecontent=fread($fp, $file_size);
fclose($fp);
$mime_boundary = ">>>>>><<<<+X[".md5(time())."]>>>><<<";
$headers .= "From: $sender\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed;\r\n";
$headers .= " boundary=\"".$mime_boundary."\"";
$msg .= "This is a multi-part message in MIME format.\r\n";
$msg .= "\r\n";
$msg .= "--".$mime_boundary."\r\n";
$msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$msg .= "Content-Transfer-Encoding: 7bit\r\n";
$msg .= "\r\n";
$msg .= "$message\r\n";
$msg .= "--".$mime_boundary."\r\n";
$msg .= "Content-Type: $file_type;\r\n";
$msg .= " name=\"$file_name\"\r\n";
$msg .= "Content-Transfer-Encoding: quoted-printable\r\n";
$msg .= "Content-Disposition: attachment;\r\n";
$msg .= " filename=\"$file_name\"\r\n";
$msg .= "\r\n";
$msg .= $filecontent;
$msg .= "\r\n";
$msg .= "--".$mime_boundary."\r\n";
mail($to, $subject, $msg, $headers, "-f ".$sender)