View Full Version : Sending Attachements

01-29-2007, 08:15 PM
I'm trying to send an attachment with an email and the attachement is recieved but its not openable. I've tried sending to yahoo and to my domain email and both have the same problem.

function sendAttached($to, $subject, $msgtext, $from, $file, $type)
$fp = fopen($file,"rb");

echo $file;
$fcontent = fread($fp ,filesize($file));
echo $fcontent;
$content = chunk_split(base64_encode($fcontent));
$sep = strtoupper(md5(uniqid(time())));
$name = basename($file);
$header = "From: $from\nReply-To: $from\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=$sep
$body .= "--$sep\r\n";
$body .= "Content-Type: text/plain\r\n";
$body .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$body .= "$msgtext\n";
$body .= "--$sep\n";
$body .= "Content-Type: $type; name=\"$name\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Disposition: attachment; filename=\"$name\"\r\n";
$body .= "$content\r\n";
$body .= "--$sep--";
echo $body;
if (mail($to, $subject, $body, $header)) {
return true;
} else {
return false;

sendAttached('me@mydomain.com', 'Test', 'Please find attached... something ', $from, $path.$filename, "text/plain", $filename);

01-29-2007, 08:54 PM
I didn't test this out, but I thought it was a good tutorial on the subject ...


01-29-2007, 10:01 PM
I tried checked that against what I had and there were a few extra line breaks which I've put in now and its still not working :s

01-30-2007, 05:54 AM
One of the things that might help is to send yourself an email with an attacment, just as you normally would in Outlook or whatever email client you use. Look at the message source which includes the headers, and see how that differs from the one you're trying to work with here.

Another option might be to use PHPMailer (http://phpmailer.sourceforge.net/).

01-30-2007, 10:36 AM
I tried that too and it all looks fine... I tihnk I'm going to have to go through this with a fine tooth comb, theres obviously some minor difference here that I'm not spotting.

01-30-2007, 02:42 PM
Nancy ...

Make a copy of your script and save as .txt ... then attach it here.

I'm thinking there's still a broken line (with carriage return), but we can't
see it with this forum. So we need to see your actual file.

01-30-2007, 03:13 PM
I found another script and thats working fine. Interestingly, it skips the part about Content-Disposition: attachment;