0

i try to create a form with a option to upload files and to get it to my email . for example that a user can put his info in the input fields and to add a file and when the user submit it i will get it all to my mail.

this is my code :

<?php

if(isset($_FILES) && (bool) $_FILES) {

$allowedExtensions = array("pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt");

$files = array();
foreach($_FILES as $name=>$file) {
    $file_name = $file['name']; 
    $temp_name = $file['tmp_name'];
    $file_type = $file['type'];
    $path_parts = pathinfo($file_name);
    $ext = $path_parts['extension'];
    if(!in_array($ext,$allowedExtensions)) {
        die("File $file_name has the extensions $ext which is not allowed");
    }
    array_push($files,$temp_name);
}

// email fields: to, from, subject, and so on
$to = "dorozenman@gmail.com";
$from = $_POST['sender_email']; 
$subject ="test attachment"; 
$message = "here ya go";
$headers = "From: $from";

// boundary 
$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

// headers for attachment 
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 

// multipart boundary 
$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 .= "--{$mime_boundary}\n";

// preparing attachments
for($x=0;$x<count($files);$x++){
    $file = fopen($files[$x],"rb");
    $data = fread($file,filesize($files[$x]));
    fclose($file);
    $data = chunk_split(base64_encode($data));
    $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
    "Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . 
    "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
    $message .= "--{$mime_boundary}\n";
}

// send

$ok = @mail($to, $subject, $message, $headers); 
if ($ok) { 
    echo "<p>mail sent to $to!</p>"; 
} else { 
    echo "<p>mail could not be sent!</p>"; 
} 
   }    

   ?>

(and file arrive named... tmp/phpcVjk4w/ ) any suggestions?

user3459398
  • 3
  • 1
  • 5
  • 1
    Obligatory warning to others: don't copy and paste such awful manual MIME/attachment construction code. There are [SwiftMailer/PHPMailer](http://stackoverflow.com/questions/303783/phpmailer-vs-swiftmailer) which simplify such tasks substantially. – mario Jun 01 '14 at 22:19

2 Answers2

0

Which is absolutely normal.

Basically, files are uploaded to your server, PHP temporary stores them in /tmp/ directory.

$files = array();
foreach($_FILES as $name=>$file) {
    $temp_name = $file['tmp_name'];
    array_push($files,$temp_name);
}

Above, you put that temporary name into an array, and below you put that name in your mail :

for($x=0;$x<count($files);$x++){
    $message .= name=\"$files[$x]\"; //...
}

What do you want your script to do?

Loïc
  • 11,804
  • 1
  • 31
  • 49
  • first thing thank you for your help, second where exactly should i place the to function above?, third i want the script to upload the file to my server & send it to my mail – user3459398 Jun 01 '14 at 22:21
  • In that case I suggest first uploading the file to the server, then attaching it to the email from the saved location. – bloodyKnuckles Jun 01 '14 at 22:45
  • thank you man! it start working by the help above i appreciate your effort as well!! – user3459398 Jun 01 '14 at 22:57
0

Change:

array_push($files,$temp_name);

to:

array_push($files, array('temp_name' => $temp_name, 'file_name' => $file_name));

And change:

// preparing attachments
for($x=0;$x<count($files);$x++){
    $file = fopen($files[$x],"rb");
    $data = fread($file,filesize($files[$x]));
    fclose($file);
    $data = chunk_split(base64_encode($data));
    $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
    "Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . 
    "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
    $message .= "--{$mime_boundary}\n";
}

To:

// preparing attachments
for($x=0;$x<count($files);$x++){
    $temp_name = $files[$x]['temp_name']; // temporary file location
    $file_name = $files[$x]['file_name']; // filename
    $file = fopen($temp_name,"rb");
    $data = fread($file,filesize($temp_name));
    fclose($file);
    $data = chunk_split(base64_encode($data));
    $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$file_name\"\n" . 
    "Content-Disposition: attachment;\n" . " filename=\"$file_name\"\n" . 
    "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
    $message .= "--{$mime_boundary}\n";
}

The difference here is pulling the filename into the $files array along with the temporary file location. You need both, where is the file on the server, and what is the name of the file when uploaded.

bloodyKnuckles
  • 11,551
  • 3
  • 29
  • 37