eMonk Posted November 3, 2008 Share Posted November 3, 2008 i'm trying to get the following code to send multiple image attachments to my email address but it's currently only sending 1. what's wrong with this code? i can't seem to send 2 images with it... i want a total of 10 jpg/png image attachments... <?php // Read POST request params into global vars $to = 'webmaster@domain.com'; $email = $_POST['email']; $subject = $_POST['subject']; // Obtain file upload vars $fileatt = $_FILES['fileatt']['tmp_name']; $fileatt_type = $_FILES['fileatt']['type']; $fileatt_name = $_FILES['fileatt']['name']; // THIS HAS BEEN REPEATED AND I HAVE CHANGED THE FILE NAME $fileatt2 = $_FILES['fileatt2']['tmp_name2']; $fileatt_type2 = $_FILES['fileatt2']['type2']; $fileatt_name2 = $_FILES['fileatt2']['name2']; $headers = "From: $email"; 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" . "msg here" ; // 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"; // THIS HAS BEEN REPEATED $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, $message, $headers); if ($ok) { echo "<p>Thank you <b>$name</b></p> <p>We have received your application and we will be in touch shortly</p>"; } else { echo "<p>Sorry, but there as an error. If the problem persists please email us at </p>"; } ?> any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/131170-multiple-form-attachments/ Share on other sites More sharing options...
JonnoTheDev Posted November 3, 2008 Share Posted November 3, 2008 Because your code is only processing the first image $fileatt. You are referencing the 2nd image in $fileatt2 = $_FILES['fileatt2']['tmp_name2']; This should be $fileatt2 = $_FILES['fileatt2']['tmp_name']; as there is no such thing as tmp_name2 You then need to add the code to include this image in your email Quote Link to comment https://forums.phpfreaks.com/topic/131170-multiple-form-attachments/#findComment-681038 Share on other sites More sharing options...
eMonk Posted November 3, 2008 Author Share Posted November 3, 2008 thx for the reply but i still can't seem to get this to work. anymore ideas? also, is there a more compact way of obtaining file upload vars instead of copying and pasting it 10 times? Quote Link to comment https://forums.phpfreaks.com/topic/131170-multiple-form-attachments/#findComment-681495 Share on other sites More sharing options...
htzone Posted November 3, 2008 Share Posted November 3, 2008 Someone may correct me on this, but if the total filesize of your attachments exceeds PHP's "upload_max_filesize" setting in php.ini, the latter attachment will be discarded (provided the first attachment doesn't exceed the upload limit as well). Sometimes you can increase this limit with an .htaccess file or by setting the variable in your script, and obviously in php.ini as well (if you have access to that). You can try adding this to your .htaccess if you have one: php_value upload_max_filesize 10485760 The value is in bytes, that particular setting puts the max filesize at 10 MB. Quote Link to comment https://forums.phpfreaks.com/topic/131170-multiple-form-attachments/#findComment-681518 Share on other sites More sharing options...
eMonk Posted November 3, 2008 Author Share Posted November 3, 2008 updated code: <?php // Read POST request params into global vars $name = $_POST['name']; $to = $_POST['to']; $email = $_POST['email']; $subject = $_POST['subject']; // Obtain file upload vars $fileatt1 = $_FILES['fileatt1']['tmp_name']; $fileatt1_type = $_FILES['fileatt1']['type']; $fileatt1_name = $_FILES['fileatt1']['name']; // THIS HAS BEEN REPEATED AND I HAVE CHANGED THE FILE NAME $fileatt2 = $_FILES['fileatt2']['tmp_name']; $fileatt2_type = $_FILES['fileatt2']['type']; $fileatt2_name = $_FILES['fileatt2']['name']; $headers = "From: $email"; if (is_uploaded_file($fileatt1)) { // Read the file to be attached ('rb' = read binary) $file1 = fopen($fileatt1,'rb'); $data1 = fread($file1,filesize($fileatt1)); fclose($file1); } if (is_uploaded_file($fileatt2)) { // Read the file to be attached ('rb' = read binary) $file2 = fopen($fileatt2,'rb'); $data2 = fread($file2,filesize($fileatt2)); fclose($file2); } // 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" . "Name: " . $name . "\n\n" . "Email: " . $email . "\n\n" ; // Base64 encode the file data $data1 = chunk_split(base64_encode($data1)); $data2 = chunk_split(base64_encode($data2)); // Add file attachment to the message $message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt1_type};\n" . " name=\"{$fileatt1_name}\"\n" . //"Content-Disposition: attachment;\n" . //" filename=\"{$fileatt1_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data1 . "\n\n" . "--{$mime_boundary}--\n"; // THIS HAS BEEN REPEATED $message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt2_type};\n" . " name=\"{$fileatt2_name}\"\n" . //"Content-Disposition: attachment;\n" . //" filename=\"{$fileatt2_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data2 . "\n\n" . "--{$mime_boundary}--\n"; // Send the message $ok = @mail($to, $subject, $message, $headers); if ($ok) { echo "<p>Thank you <b>$name</b></p> <p>We have received your application and we will be in touch shortly</p>"; } else { echo "<p>Sorry, but there as an error. If the problem persists please email us at </p>"; } ?> i tried uploading 2 small gif files but only the first one is being sent, the second one isn't. i have also searched google for clues but this seems to be a big problem in php and seems no one has a solid answer to this. there are some scripts such as phpmailer that allows multiple file upload but when i tried installing it and running the test form, it sent it twice so i don't want to bother with it. its also hard to read and would like a compact version for personal use. why isn't the second file being attached to emails with the above code? Quote Link to comment https://forums.phpfreaks.com/topic/131170-multiple-form-attachments/#findComment-681585 Share on other sites More sharing options...
JonnoTheDev Posted November 4, 2008 Share Posted November 4, 2008 i have also searched google for clues but this seems to be a big problem in php There is no big issue as you say with uploading multiple files. The max upload file size is usually set to 2 or 4mb by default. If error reporting is enabled then you would see an message warning that the file is too large. Add an .htaccess file to your webroot to increase this using: php_value upload_max_filesize 200M php_value post_max_size 200M To be honest you are using the mail() funtion to send attachments which isnt the best. Use PEAR:Mail_Mime package or install the SwiftMailer libraries to easily add attachments to your email. In terms of file uploading check the basics. Do your form fields have the correct names for both images: fileatt1 & fileatt2 This code will upload your files to the directory specified, saving the files as image1.x and image2.x You can then easily check for the existance of the files and attach them to email (not using mail()) // make sure that this directory has write permissions $uploadPath = '/path/to/uploads/'; for($x = 1 $x <= 2; $x++) { // check that an image has been selected if(strlen($_FILES['fileatt'.$x]['name'])) { $fileType = strtolower(strrchr($_FILES['fileatt'.$x]['name'], ".")); $newFileName = "image".$x.".".$fileType; $uploadTo = $uploadPath.$newFileName; move_uploaded_file($_FILES['fileatt'.$x]['tmp_name'], $uploadTo); } } Quote Link to comment https://forums.phpfreaks.com/topic/131170-multiple-form-attachments/#findComment-681911 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.