Php Developer wrote:

I found a good function at that sends email with atachment, but 
unfortunately i still cannot figure out how to send a body of the email too. 
Here is the code:

function sendEmailWithAttachement($to, $subject, $message, $file)
  if (strtoupper(substr(PHP_OS,0,3)=='WIN')) {
} elseif (strtoupper(substr(PHP_OS,0,3)=='MAC')) {
else {
} $filename = basename($file);

What does the following do?  Does it echo something to the screen?

  $file_size = filesize($file);
  $file_type = filetype($file);

Shouldn't you be checking to see if the file exists and is readable before you try opening it?

  $handle = fopen($file, "rb");
  $content = fread($handle, $file_size);
  $content = chunk_split(base64_encode($content));

You should make sure ( strlen($content) !== 0 ) at this point.

  # Common Headers
$headers = 'From:'.$eol;
$headers .= 'Reply-To:'.$eol;
$headers .= 'Return-Path:'.$eol;     // these two to set 
reply address
$headers .= "Message-ID: <".time()." 
$headers .= "X-Mailer: PHP v".phpversion().$eol;           // These two to help 
avoid spam-filters
# Boundry for marking the split & Multitype Headers
$headers .= 'MIME-Version: 1.0'.$eol;
$headers .= "Content-Type: multipart/related; 
$msg = "";

# Attachment
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: ".$file_type."; name=\"".$filename."\"".$eol;   // 
sometimes i have to send MS Word, use 'msword' instead of 'pdf'
$msg .= "Content-Transfer-Encoding: base64".$eol;
$msg .= $message;

Looks like you are missing some line endings on the above line.

Plus, why do the following lines require two EOL's but the above lines only use one EOL?

$msg .= "Content-Disposition: attachment; 
filename=\"".$filename."\"".$eol.$eol; // !! This line needs TWO end of lines !! 
$msg .= $content.$eol.$eol;
# Setup for text OR html
$msg .= "Content-Type: multipart/alternative".$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 
$msg .= "+ + Text Only + +".$eol.$eol;
# Finished
$msg .= "--".$mime_boundary."--".$eol.$eol;   // finish with two eol's for 
better security. see Injection.
mail($to, $subject, $msg, $headers);

Jim Lucas

A: Maybe because some people are too annoyed by top-posting.
Q: Why do I not get an answer to my question(s)?
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?

PHP General Mailing List (
To unsubscribe, visit:

Reply via email to