Multiple Attachments from form to email

Discussion in 'PHP' started by zaya, Oct 12, 2009.

  1. #1
    
    
    <?php
    // Read POST request params into global vars
    $to      = $_POST['to'];
    $email   = $_POST['email'];
    $subject = $_POST['subject'];
    $name = $_POST['name'];
    $phnnumb   = $_POST['phnnumb'];
    $cell = $_POST['cell'];
    $email = $_POST['email'];
    $address = $_POST['address'];
    ETC...
    
     // array with filenames to be sent as attachment
       
          $files = array(2);
    	  $fileatt_type = array(2);
    	  $fileatt_name = array(2);
    	  $data = array(2);
    
    // Obtain file upload vars
    $files[0]      = $_FILES['fileatt']['tmp_name'];
    $fileatt_type[0] = $_FILES['fileatt']['type'];
    $fileatt_name[0] = $_FILES['fileatt']['name'];
    
    // Obtain file secoond upload vars
    $files[1]      = $_FILES['fileatt2']['tmp_name'];
    $fileatt_type[1] = $_FILES['fileatt2']['type'];
    $fileatt_name[1] = $_FILES['fileatt2']['name'];
    
    for ($x=0;$x<=count($files);$x++)
    {
    if (is_uploaded_file($files[$x])) {
      // Read the file to be attached ('rb' = read binary)
      $file = fopen($files[$x],'rb');
      $data = fread($file,filesize($files[$x]));
      fclose($file);
    
     
      
      // Add file attachment to the message
      $message .= "--{$mime_boundary}\n" .
                  "Content-Type: {$fileatt_type[$x]};\n" .
                  " name=\"{$fileatt_name[$x]}\"\n" .
                  //"Content-Disposition: attachment;\n" .
                  //" filename=\"{$fileatt_name[$x]}\"\n" .
                  "Content-Transfer-Encoding: base64\n\n" .
                  $data . "\n\n" .
                  "--{$mime_boundary}--\n";           
    
       
    }
    }
    
     // 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}\"";
    // Base64 encode the file data
      $data = chunk_split(base64_encode($data));
    			  
      // 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" .
                 "Phone Number: " . $phnnumb . "\n\n" .
                 "Cell Phone: " . $cell . "\n\n" .
                 "Email Address: " . $email . "\n\n" .
                 "Physical Address: " . $address . "\n\n" .
                 "ETC...(more fields)" . "\n\n";
    
    // Send the message
    $ok = @mail($to, $subject, $message, $headers);
    if ($ok) {
      echo "<p align='center'><img src='Head_Banner2.jpg' alt='Test' width='520' height='85' /></p>
      <p align='center'>Thank you <b>$name</b>
     for advertising with us! Your property will be uploaded on to the website within few days and an email confirmation will be sent to you.</p>";
      
    } else {
      echo "<p><font size='12'>Sorry, but there as an error. If the problem persists please email us</font></p>";
    }
    ?>
    <p align="center">  <input name="button2" type="button" style="height: 25px; width: 110px" onclick="javascript:window.close();" value="Close Window" /></p>
    <br>
    
    Code (markup):

     
    zaya, Oct 12, 2009 IP