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.
[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/vnd.ms-word";
} else if ( preg_match("/\.(xls)$/",$filename) ) { $mimetype = "application/vnd.ms-excel";
} 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;
}
}
sleep(0.25);
sendmsg($recipient, $subject, $contact_info, $froms_, $target_path, $mimetype);
[/PHP]
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!