I pulled this from a user comment via php.net, but it's not working quite like it should. I'm trying to attach an error log to an e-mail, suitable for HTML or plain text mail. I'll keep playing, but perhaps someone better at mail than I can spot the errors.
The problems:
1. the attachment does not have the actual content in it; it only shows
Content-Type: text/plain
2. the attachment, about 3kb, always shows 0.0kb.
3. Message output to Entourage mail program shows the following:
This is a multi-part message in MIME format.
If you are reading this, please update your email-reading software.
f”-ŠÙrþØ*•àn‡r7¬´Û¡Ü€ømšP
I guess this script was meant for a binary attachment (PDF or Word), and I can't translate it to attach a simple text file!
if (strtoupper(substr(PHP_OS,0,3)=='WIN')) {
$eol="\r\n";
} elseif (strtoupper(substr(PHP_OS,0,3)=='MAC')) {
$eol="\r";
} else {
$eol="\n";
}
# File for Attachment
$f_name=$log; // use relative path OR ELSE big headaches. $letter is my file for attaching.
$handle=fopen($f_name, 'r');
$f_contents=fread($handle, filesize($f_name));
$f_contents=chunk_split(base64_encode($f_contents)); //Encode The Data For Transition using base64_encode();
$f_type=filetype($f_name);
fclose($handle);
# To Email Address
$emailaddress="somewhere@somewhere.com";
# Message Subject
$emailsubject="Error from host.com ".$time;
# Message Body
//ob_start();
//require("emailbody.php"); // i made a simple & pretty page for showing in the email
//$body=ob_get_contents(); ob_end_clean();
$body = "<html><title>HI</title><body>Test</body></html>";
# Common Headers
$headers .= 'From: Webmaster <Webmaster@host.com>'.$eol;
$headers .= 'Reply-To: Webmaster <Webmaster@host.com>'.$eol;
$headers .= 'Return-Path: Webmaster <Webmaster@host.com>'.$eol; // these two to set reply address
$headers .= "Message-ID: <".$now." TheSystem@".$_SERVER['SERVER_NAME'].">".$eol;
$headers .= "X-Mailer: PHP v".phpversion().$eol; // These two to help avoid spam-filters
# Boundry for marking the split & Multitype Headers
$mime_boundary=md5(time());
//$headers .= "Content-Type: text/plain; boundary=\"".$mime_boundary."\"".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$mime_boundary."\"".$eol;
$headers .= 'MIME-Version: 1.0'.$eol;
$msg = "";
# Attachment
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: text/plain; name=\"".$logfile."\"".$eol; // sometimes i have to send MS Word, use 'msword' instead of 'pdf'
$msg .= "Content-Transfer-Encoding: 8bit".$eol;
$msg .= "Content-Disposition: attachment; filename=\"".$logfile."\"".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !!
$msg .= $f_contents.$eol.$eol;
# Setup for text OR html
$msg .= "Content-Type: text/plain".$eol;
# Text Version
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol;
$msg .= "This is a multi-part message in MIME format.".$eol;
$msg .= "If you are reading this, please update your email-reading software.".$eol;
$msg .= "+ + Text Only Email from webmaster@rw-pw.com + +".$eol.$eol;
# HTML Version
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: text/html; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: base64".$eol;
$msg .= $body.$eol.$eol;
# Finished
$msg .= "--".$mime_boundary."--".$eol.$eol; // finish with two eol's for better security. see Injection.