View Single Post
Old 11-03-2008, 11:24 PM  
eMonk
Confirmed User
 
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
Coders: How can I send multiple attachments via form to email with php 5

i would like to send 10 image files from my html form to php but having some problems & i've been searching google all day with no solid leads & was hoping that someone here would know because i know this has been done many times before. here's my php code:

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>";
}

?>
this code only sends the first file and not the second, any ideas?
eMonk is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote