tags:

views:

41

answers:

2

I created "uploads" folder on the server, and a file uploaded by a user on the HTML form, gets into that folder fine. Next, I try to retrieve it from that folder and attach it to an email, but this part doesn't work. Here is my PHP:

    <?php
$name = $_POST['name'];
$email = $_POST['email'];
$form_subject = $_POST['form_subject'];
$message = $_POST['message'];


//File upload

// Where the file is going to be placed 
$target_path = "uploads/";

/* Add the original filename to our target path.  
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}   

//End of file upload


$ip=@$REMOTE_ADDR;
$date=date("l, F j, Y, g:i a"); 

$to = "[email protected]";
$subject = "New data submitted!";
$body = "Here is the information submitted to 
www.polycysticliverdisease.com/html/pld_form.php 
from $ip on $date.\n\n
--------------------------------\n\n
name: $name \n\n
email address: $email \n\n
subject: $form_subject \n\n
comment: $message";
//mail($to, $subject, $body);

// Obtain file upload vars    
$fileatt      = $_FILES['uploadedfile']['tmp_name'];    
$fileatt_type = $_FILES['uploadedfile']['type'];    
$fileatt_name = $_FILES['uploadedfile']['name'];

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 .= "\nMIME-Version: 1.0\n" .    
             "Content-Type: multipart/mixed;\n" .    
             " boundary=\"{$mime_boundary}\"";

// Add a multipart boundary above the plain message    
 $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";

// Base64 encode the file data    
 $data = chunk_split(base64_encode($data));

 // Add file attachment to the message    
 $message .= "--{$mime_boundary}\n" .    
             "Content-Type: {$fileatt_type};\n" .    
             " name=\"{$fileatt_name}\"\n" .    
             "Content-Disposition: attachment;\n" .    
             " filename=\"{$fileatt_name}\"\n" .    
             "Content-Transfer-Encoding: base64\n\n" .    
             $data . "\n\n" .    
             "--{$mime_boundary}--\n";    
}

// Send the message    
$ok = @mail($to, $subject, $body, $headers);    
if ($ok) {    
 echo "<p>Mail sent! Yay PHP!</p>";    
} else {    
 echo "<p>Mail could not be sent. Sorry!</p>";    
}

?>

Here is my HTML file upload control:

<input type="submit" name="submit" class="submit" />
<input type="file" name="uploadedfile" class="upload" />

Thank you!

+2  A: 

I'd use some mailing library - maybe Swiftmailer. Programming it yourself isn't worth it (if you're not doing it for studying purposes of course).

Kaltas
Thank you so, so much!It worked beautifully. I love Swiftmailer!But when I set up a body for my email, I use a mix of text and variables:$message->setBody('Here is the information submitted to www.polycysticliverdisease.com/html/pld_form.php from $ip on $date.\n\nname: $name \n\nemail address: $email \n\nsubject: $form_subject \n\ncomment: $form_message', 'text/html');All variables are declared and assigned proper values.But they and the '\n\n' all appear in the sent email as part of the text, i.e. they are not recognized as variables.Why?
vlevsha
@vlevsha because you are using single quotes. Use double quotes.
Unicron
Thank you, I did even before I saw your answer. :) Now, I've got a much more serious problem: http://stackoverflow.com/questions/3290966/retrieving-a-file-name-to-attach-to-an-email-with-swiftmailer-and-php
vlevsha
A: 

Use a pre-built mailer package like SwiftMailer or PHPMailer. Both can handle attachments easily.

Unicron
Thank you! I chose SwiftMailer.Just loved it!
vlevsha