By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,870 Members | 2,400 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,870 IT Pros & Developers. It's quick & easy.

PHP 4.1.1 and Email Attachments

P: 3
Now I've been working on a script that sends emails with different attachments, including csv, xls, docs, txt, and html/htm files. However when attaching, some information at the top in the txt files are missing, and not only that, doc / xls files are unable to open without a slew of funny characters when running through [PHP]chunk_split(base64_encode($filename))[/PHP] to attach. The attachment is there, the message is there. However opening the applications is a whole different story! The script is below. My suspicion is I'm using a very a outdated version of PHP (hopefully will be upgraded soon!). Could this be the culprit? Otherwise, I'm stumped on the inconsistent handling of attachments using PHP.


if (preg_match("/\.(txt)$/",$filename) ) { $mimetype = "text/txt";
} else if ( preg_match("/\.(csv)$/",$filename) ) { $mimetype = "text/csv";
} else if ( preg_match("/\.(htm)$/",$filename) ) { $mimetype = "text/htm";
} else if ( preg_match("/\.(html)$/",$filename) ) { $mimetype = "text/html";
} else if ( preg_match("/\.(doc)$/",$filename) ) { $mimetype = "application/";
} else if ( preg_match("/\.(xls)$/",$filename) ) { $mimetype = "application/";
} else { $mimetype = "text/txt"; }

function sendmsg($to, $subject, $msgtext, $from, $file, $type)

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

sendmsg($recipient, $subject, $contact_info, $froms_, $target_path, $mimetype);


Yes the attachment code is now commented because I have it working to send emails only with a link to the document instead of an attachment. But All i did was comment out instead of rewrite code. BIGUPS and appreciation!
May 25 '07 #1
Share this Question
Share on Google+
3 Replies

Expert 2.5K+
P: 3,235
I think you will need another newline before the content.
May 25 '07 #2

P: 3
I think you will need another newline before the content.
Jeezz, you are correct. Lesson learned! thnx
May 25 '07 #3

Expert 2.5K+
P: 3,235
Jeezz, you are correct. Lesson learned! thnx
No problem. Welcome to The Scripts.
May 26 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.