Attach file from server

Hi guys…!!
can anyone give me some idea how to attach a file from the server and send it as attachment through email??
i have the following code:


<?php
// Read POST request params into global vars
$to      = $_POST['to'];
$from    = $_POST['from'];
$subject = $_POST['subject'];
$message = $_POST['message'];
// Obtain file upload vars
$fileatt      = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];
$headers = "From: $from";
if (is_uploaded_file($fileatt)) {
  // Read the file to be attached ('rb' = read binary)
  $file = fopen($fileatt,'rb');
  $data = fread($file,filesize($fileatt));
  fclose($file);
  // Generate a boundary string
  $semi_rand = md5(time());
  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  // Add the headers for a file attachment
  $headers .= "\
MIME-Version: 1.0\
" .
              "Content-Type: multipart/mixed;\
" .
              " boundary=\\"{$mime_boundary}\\"";
  // Add a multipart boundary above the plain message
  $message = "This is a multi-part message in MIME format.\
\
" .
             "--{$mime_boundary}\
" .
             "Content-Type: text/plain; charset=\\"iso-8859-1\\"\
" .
             "Content-Transfer-Encoding: 7bit\
\
" .
             $message . "\
\
";
  // Base64 encode the file data
  $data = chunk_split(base64_encode($data));
  // Add file attachment to the message
  $message .= "--{$mime_boundary}\
" .
              "Content-Type: {$fileatt_type};\
" .
              " name=\\"{$fileatt_name}\\"\
" .
              //"Content-Disposition: attachment;\
" .
              //" filename=\\"{$fileatt_name}\\"\
" .
              "Content-Transfer-Encoding: base64\
\
" .
              $data . "\
\
" .
              "--{$mime_boundary}--\
";
}
// Send the message
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
  echo "<p>Mail sent! Yay PHP!</p>";
} else {
  echo "<p>Mail could not be sent. Sorry!</p>";
}
?>

but this code does not attach file from the server.
please help me out… thanks in advance…!!

Hi php_rockzz,

I would recommend that you look at Swift Mailer. This is an email library that is simple to implement and performs email attachments much simpler than what your are trying to do in your script. There is a 10 part tutorial on DevShed where [URL=“http://www.devshed.com/c/a/PHP/Dynamically-Attaching-Files-with-Swift-Mailer/”]this part shows how to do dynamic attachments.

Regards,
Steve