file sending via email but not uploading

A

Anonymous

Guest
hi,

i had the following code working on a windows server with php and apache:

Code:
<?php

// set variables

error_reporting(E_ALL);

$uploaddir = 'C:/uploads//';//extra slash needed for escape char.
$newfile = $uploaddir . $_FILES['userfile']['name'];
$to ="luke@lukem.co.uk";
$subject = "Feedback from Caring-People Site";
$from = $_POST['email'];
$address = $_POST['address'];
$birth = $_POST['birth'];
$phone = $_POST['phone'];
//$message =$_POST['message'];

$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
$tmp_name = $_FILES['userfile']['tmp_name'];
$type = $_FILES['userfile']['type'];
$name = $_FILES['userfile']['name'];
$size = $_FILES['userfile']['size'];
 
 
//check if the file exists, then upload it and read it in to a variable for attachment.


if(file_exists($tmp_name)){
move_uploaded_file($_FILES['userfile']['tmp_name'], $newfile);
$file = fopen($newfile,'rb'); // open the file for a binary read
$data = fread($file,filesize($tmp_name)); // read the file content into a variable
fclose($file); // close the file

// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data)); 
     

} 

      // now we'll build the message headers
      $headers = "From: $from\r\n" .
         "MIME-Version: 1.0\r\n" .
         "Content-Type: multipart/mixed;\r\n" .
         " boundary=\"{$mime_boundary}\"";

      // next, we'll build the message body
      // note that we insert two dashes in front of the
      // MIME boundary when we use it
      $message = "This is a multi-part message in MIME format.\n\n" .
         "--{$mime_boundary}\n" .
         "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
         "Content-Transfer-Encoding: 7bit\n\n" .
         $message . "\n\n";

$message .= "Application for: ".$_POST["position"] ."\n\n";
$message .= "Full Name: ".$_POST["name"] ."\n\n";
$message .= "Sender's Postal Address: ".$_POST["address"] . "\n\n";
$message .= "Phone Number: ".$_POST["phone"] . "\n\n";
$message .= "Date of Birth: ".$_POST["birth"] ."\n\n";
$message .= "Message body: ". $_POST["message"] ."\n\n";

      // now we'll insert a boundary to indicate we're starting the attachment
      // we have to specify the content type, file name, and disposition as
      // an attachment, then add the file content and set another boundary to
      // indicate that the end of the file has been reached
      $message .= "--{$mime_boundary}\n" .
         "Content-Type: {$type};\n" .
         " name=\"{$name}\"\n" .
         "Content-Disposition: attachment;\n" .
         //" filename=\"{$fileatt_name}\"\n" .
         "Content-Transfer-Encoding: base64\n\n" .
         $data . "\n\n" .
         "--{$mime_boundary}--\n";

      // now we just send the message
      if (@mail($to, $subject, $message, $headers))
         header("Location: thanks.htm");
         
      else
         header("Location: error.htm");
   

?>

the attachment was sending fine and being uploaded to a folder locally (uploads under htdocs)

however, when i upload the code to my live server (linux with php and apache), i can get the attachment to send, but it does not appear in the upload folder.

relevant code is as follows (the rest is unchanged):

Code:
$uploaddir = '/uploads';
$newfile = $uploaddir . $_FILES['userfile']['name'];

i have tried a number of other directory paths but with no success. if i change it to, for example '/htdocs/cp/uploads', an empty file arrives via email and nothing is uploaded.

i have tried setting the permissions on uploads to 600 and 777 but that didn't help.

does anyone have any idea what might be causing this?

many thanks,

luke.
 
this is resolved now, but i now get this error if i don't include an attachment:

Notice: Undefined variable: data in /home/fhlinux196/l/lukem.co.uk/user/htdocs/cp/form2mail.php on line 72

Warning: Cannot modify header information - headers already sent by (output started at /home/fhlinux196/l/lukem.co.uk/user/htdocs/cp/form2mail.php:72)
in /home/fhlinux196/l/lukem.co.uk/user/htdocs/cp/form2mail.php on line 77

revised code:

Code:
$uploaddir = $_SERVER['DOCUMENT_ROOT']."/cp/uploads/";
$newfile = $uploaddir . $_FILES['userfile']['name'];
$to ="luke@lukem.co.uk";
$subject = "Feedback from Caring-People Site";
$from = $_POST['email'];
$address = $_POST['address'];
$birth = $_POST['birth'];
$phone = $_POST['phone'];
//$message =$_POST['message'];

$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
$tmp_name = $_FILES['userfile']['tmp_name'];
$type = $_FILES['userfile']['type'];
$name = $_FILES['userfile']['name'];
$size = $_FILES['userfile']['size'];


//check if the file exists, then upload it and read it in to a variable for attachment.


if(is_uploaded_file($tmp_name)){ move_uploaded_file($_FILES['userfile']['tmp_name'], $newfile); $file = fopen($newfile,'rb'); // open the file for a binary read $data = fread($file,filesize($newfile)); // read the file content into a variable fclose($file); // close the file

// now we encode it and split it into acceptable length lines $data = chunk_split(base64_encode($data));


}

      // now we'll build the message headers
      $headers = "From: $from\r\n" .
         "MIME-Version: 1.0\r\n" .
         "Content-Type: multipart/mixed;\r\n" .
         " boundary=\"{$mime_boundary}\"";

      // next, we'll build the message body
      // note that we insert two dashes in front of the
      // MIME boundary when we use it
      $message = "This is a multi-part message in MIME format.\n\n" .
         "--{$mime_boundary}\n" .
         "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
         "Content-Transfer-Encoding: 7bit\n\n" .
         $message . "\n\n";

$message .= "Application for: ".$_POST["position"] ."\n\n"; $message .= "Full Name: ".$_POST["name"] ."\n\n"; $message .= "Sender's Postal Address: ".$_POST["address"] . "\n\n"; $message .= "Phone Number: ".$_POST["phone"] . "\n\n"; $message .= "Date of Birth: ".$_POST["birth"] ."\n\n"; $message .= "Message body: ". $_POST["message"] ."\n\n";

      // now we'll insert a boundary to indicate we're starting the attachment
      // we have to specify the content type, file name, and disposition as
      // an attachment, then add the file content and set another boundary to
      // indicate that the end of the file has been reached
      $message .= "--{$mime_boundary}\n" .
         "Content-Type: {$type};\n" .
         " name=\"{$name}\"\n" .
         "Content-Disposition: attachment;\n" .
         //" filename=\"{$fileatt_name}\"\n" .
         "Content-Transfer-Encoding: base64\n\n" .
         $data . "\n\n" .
         "--{$mime_boundary}--\n";

      // now we just send the message
      if (@mail($to, $subject, $message, $headers)){
         header("Location: thanks.htm");
   }
      else{
         header("Location: error.htm");
   }

?>

any idea what i need to do to make this work regardless of whether the user includes an attachment?

thanks,

luke.
 
Well, on line 72 you refer to $data, but $data is never set anywhere in your code.
 
doesn't this count then?

$data = chunk_split(base64_encode($data));
 
The only place where that appears in your code, it's commented out.
 
it looks like that the way the code has come through in the post, but it isnt really commented out:

$file = fopen($newfile,'rb'); // open the file for a binary read
$data = fread($file,filesize($newfile)); // read the file content into a variable
fclose($file); // close the file

// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
 
i think i need a way of only filling that variable if there is a file, otherwise skipping it.

any ideas how i do that?

thanks,

lukemack.
 
many thanks for your replies. i've got it working now.

lukemack.
 
Back
Top