简体   繁体   中英

mail attachment issue - why do i dont get the file properly?

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?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM