Send email from form with multiple attachments
Posted: Wed Dec 10, 2008 2:41 pm
Hello, I'm very new to PHP so forgive my noobness. Currently I have a form page that has the user enter in their info and attach files to be sent to an email. So far I've been able to get it to send one attachment, but I'm lost as so how I can send more. These are the variables for the attachments:
$file1_name=$_FILES["file1"]["name"];
$file1_temp=$_FILES["file1"]["tmp_name"];
$file2_name=$_FILES["file2"]["name"];
$file2_temp=$_FILES["file2"]["tmp_name"];
$file3_name=$_FILES["file3"]["name"];
$file3_temp=$_FILES["file3"]["tmp_name"];
$file4_name=$_FILES["file4"]["name"];
$file4_temp=$_FILES["file4"]["tmp_name"];
$file5_name=$_FILES["file5"]["name"];
$file5_temp=$_FILES["file5"]["tmp_name"];
The field names for the attachments on the form are file1-file5, here's my current code starting on my last else if (everything leading up to this is just validation of fields):
So as you can see its set up to only send file1, how do I make it capable of sending the other 4?
$file1_name=$_FILES["file1"]["name"];
$file1_temp=$_FILES["file1"]["tmp_name"];
$file2_name=$_FILES["file2"]["name"];
$file2_temp=$_FILES["file2"]["tmp_name"];
$file3_name=$_FILES["file3"]["name"];
$file3_temp=$_FILES["file3"]["tmp_name"];
$file4_name=$_FILES["file4"]["name"];
$file4_temp=$_FILES["file4"]["tmp_name"];
$file5_name=$_FILES["file5"]["name"];
$file5_temp=$_FILES["file5"]["tmp_name"];
The field names for the attachments on the form are file1-file5, here's my current code starting on my last else if (everything leading up to this is just validation of fields):
Code: Select all
else if (is_uploaded_file($file1_temp)) { //Do we have a file uploaded?
$body = '
Shipping
First Name: '.$sfn.'
Last Name: '.$sln.'
Company/Organization: '.$sco.'
Address: '.$sad.'
Floor/Suite/Department: '.$sfsd.'
City: '.$sc.'
State: '.$sst.'
Zip: '.$szip.'
Phone Number: '.$sp.'
E-mail: '.$se.'
Billing
First Name: '.$bfn.'
Last Name: '.$bln.'
Company/Organization: '.$bco.'
Address: '.$bad.'
Floor/Suite/Department: '.$bfsd.'
City: '.$bc.'
State:'.$bst.'
Zip: '.$bzip.'
Phone Number: '.$bp.'
Comments
Comments: '.$comments.'
Order
Type: '.$t1.'
Quantity: '.$q1.'';
$fp = fopen($file1_temp, "rb"); //Open it
$data = fread($fp, filesize($file1_temp)); //Read it
$data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can emailed
fclose($fp);
//Let's start our headers
$headers = "From: $se\n";
$headers .= "Reply-To: $se\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n";
$headers .= "X-Sender: $sfn + $sln<" . $_POST['se'] . ">\n";
$headers .= "X-Mailer: PHP4\n";
$headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal
$headers .= "Return-Path: <" . $_POST['se'] . ">\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= "------=MIME_BOUNDRY_main_message \n";
$headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n";
$message .= "------=MIME_BOUNDRY_message_parts\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: quoted-printable\n";
$message .= "\n";
//* Add our message, in this case it's plain text. You could also add HTML by changing the Content-Type to text/html */
$message .= "$body\n";
$message .= "\n";
$message .= "------=MIME_BOUNDRY_message_parts--\n";
$message .= "\n";
$message .= "------=MIME_BOUNDRY_main_message\n";
$message .= "Content-Type: application/octet-stream;\n\tname=\"" . $file1_name . "\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: attachment;\n\tfilename=\"" . $file1_name . "\"\n\n";
$message .= $data; //The base64 encoded message
$message .= "\n";
$message .= "------=MIME_BOUNDRY_main_message--\n"; //final code to send the message
mail("email removed for privacy", $subject, $message, $headers);
echo "The file was successfully sent!<br/><a href='index2.php'>Click here to return to our homepage</a>";
}