Send email from form with multiple attachments

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chris8585
Forum Newbie
Posts: 4
Joined: Wed Dec 10, 2008 2:33 pm

Send email from form with multiple attachments

Post by chris8585 »

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):

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>"; 
}
 
So as you can see its set up to only send file1, how do I make it capable of sending the other 4?
User avatar
desperado
Forum Commoner
Posts: 46
Joined: Wed Dec 10, 2008 8:49 am

Re: Send email from form with multiple attachments

Post by desperado »

I think you need to loop the process with foreach.
look here:

http://apptools.com/phptools/forms/forms5.php
chris8585
Forum Newbie
Posts: 4
Joined: Wed Dec 10, 2008 2:33 pm

Re: Send email from form with multiple attachments

Post by chris8585 »

I used the code from that link, it says the message is being sent but now I'm not even getting an email. This code is starting from the end of my first if statement (validation).

Code: Select all

 
else { sendmail();}
   
   function sendmail() {
   
   if ($_SERVER['REQUEST_METHOD']=="POST"){
   // we'll begin by assigning the To address and message subject
   $to="myemail";
   $subject="subject";
 
   // get the sender's name and email address
   // we'll just plug them a variable to be used later
   $from = stripslashes($_POST['sfn' + 'sln'])."<".stripslashes($_POST['se']).">";
 
   // generate a random string to be used as the boundary marker
   $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
 
   // now we'll build the message headers
   $headers = "From: $from\r\n" .
   "MIME-Version: 1.0\r\n" .
      "Content-Type: multipart/mixed;\r\n" .
      " boundary=\"{$mime_boundary}\"";
 
   // here, we'll start the message body.
   // this is the text that will be displayed
   // in the e-mail
   $message='
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.'';
 
   // next, we'll build the invisible portion of the message body
   // note that we insert two dashes in front of the MIME boundary 
   // when we use it
   $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";
 
   // now we'll process our uploaded files
   foreach($_FILES as $userfile){
      // store the file information to variables for easier access
      $tmp_name = $userfile['tmp_name'];
      $type = $userfile['type'];
      $name = $userfile['name'];
      $size = $userfile['size'];
 
      // if the upload succeded, the file will exist
      if (file_exists($tmp_name)){
 
         // check to make sure that it is an uploaded file and not a system file
         if(is_uploaded_file($tmp_name)){
    
            // open the file for a binary read
            $file = fopen($tmp_name,'rb');
    
            // read the file content into a variable
            $data = fread($file,filesize($tmp_name));
 
            // close the file
            fclose($file);
    
            // now we encode it and split it into acceptable length lines
            $data = chunk_split(base64_encode($data));
         }
    
         // now we'll insert a boundary to indicate we're starting the attachment
         // we have to specify the content type, file name, and disposition as
         // an attachment, then add the file content.
         // NOTE: we don't set another boundary to indicate that the end of the 
         // file has been reached here. we only want one boundary between each file
         // we'll add the final one after the loop finishes.
         $message .= "--{$mime_boundary}\n" .
            "Content-Type: {$type};\n" .
            " name=\"{$name}\"\n" .
            "Content-Disposition: attachment;\n" .
            " filename=\"{$fileatt_name}\"\n" .
            "Content-Transfer-Encoding: base64\n\n" .
         $data . "\n\n";
      }
   }
   // here's our closing mime boundary that indicates the last of the message
   $message.="--{$mime_boundary}--\n";
   // now we just send the message
   if (@mail($to, $subject, $message, $headers))
      echo "Message Sent";
   else
      echo "Failed to send";
}
}
 
chris8585
Forum Newbie
Posts: 4
Joined: Wed Dec 10, 2008 2:33 pm

Re: Send email from form with multiple attachments

Post by chris8585 »

hmm I just changed the $to variable to single quotes and it worked.
Post Reply