SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple Attachments from a form to email

    I am new in PHP programming. I have a html / Javascript form that needs to submit multiple (6) Picture attachments.

    When I submit the form, the form is submitted with no Errors but no attachments arrive on the email. I am using arrays to accept the attachments. The PHP code below is only trying to comprehend 2 attachments to start with but even this does not arrive on the email. I do receive all the fields from the HTML / Javascript form on the email. Only the attachments do not arrive.

    Can anyone assist with this PHP coding? I would highly appreciate anyones help and ideas.

    PHP Code:
    <html>
    <head>
    </head>


    <body style= "color: black;"  

    <?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>

  2. #2
    SitePoint Addict Mal Curtis's Avatar
    Join Date
    Jul 2009
    Location
    New Zealand
    Posts
    327
    Mentioned
    13 Post(s)
    Tagged
    0 Thread(s)
    You're generating your $mime_boundary variable too late - after you've tried to use it. As a result it won't be creating correct boundaries. I'd make sure you have your $mime_boundary var created first and see if that changes things.

  3. #3
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello Mal,

    Thanks for your suggestion. I tried it out and below is how the code looks like after the change.

    It still does not working. I can receive all the fields from the form except the attachments. Therefore I still have the same problem. Have I defined the variable ($mime_boundary) in the wrong place? please suggest.

    Code:
    <html>
    <head
    </head>
    
    
    <body style= "color: black;"  
    
    <?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...
    
    // mime boundary is initialised
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
    
     // 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());
      
     // Add the headers for a file attachment
      $headers .= "\n MIME-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..." . "\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! </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>
    
    </body>
    </html>

  4. #4
    SitePoint Addict Mal Curtis's Avatar
    Join Date
    Jul 2009
    Location
    New Zealand
    Posts
    327
    Mentioned
    13 Post(s)
    Tagged
    0 Thread(s)
    Following back you're now referencing $semi_rand when it hasn't been created. Move that up before the $mime_boundary declaration too.

    The way these multi part boundary emails work is to create a unique(ish) 'boundary' which is used in multiple places in the email to separate out content areas. The mime_boundary variable is used to create this, along with the semi_rand which is a random(ish) value.

    Also I think the for loop should be $x < count($files) rather than $x <= count($files) - and technically speaking for optimisations sake the count(files) result should be put into a variable so it's not run every time the thing loops - but that's nitpicking and wouldn't affect the outcome.

    Try moving the semi_rand value up and see what happens.

  5. #5
    SitePoint Addict Mal Curtis's Avatar
    Join Date
    Jul 2009
    Location
    New Zealand
    Posts
    327
    Mentioned
    13 Post(s)
    Tagged
    0 Thread(s)
    Oh and make sure the form which is doing the post has an enctype of "multipart/form-data" (I think that's the one, off the top of my head. It looks very similar to that - google to make sure). If it's not set to that then $_FILES would be empty as the data would not be being sent.

  6. #6
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    NO Luck. Its still not working. The updated code after the changes is below. I have put the enctype of "multipart/form-data" on the HTML form from the start because this form is working with attaching one file. It does email but Multiple does not work.

    I do apologise for the semi_rand initialisation was done below the mime_boundary. I overlooked it while making the changes. Silly Mistake!!

    PHP Code:
    <?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'];
    $area $_POST['area'];
    ETC...


    // Generate a boundary string
      
    $semi_rand md5(time());
      
    $mime_boundary "==Multipart_Boundary_x{$semi_rand}x";
      
     
    // 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";           

       
    }
    }

      
     
    // Add the headers for a file attachment
      
    $headers .= "\n MIME-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....";

    // Send the message
    $ok = @mail($to$subject$message$headers);
    if (
    $ok) {
      echo 
    "<p align='center'><img src='Head_Banner2.jpg' alt='Tanzania Real Estate' 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>";
    }
    ?>

  7. #7
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Getting the MIME right is difficult. Why not use the Pear library instead of reinventing the wheel?
    See a sample here:
    Email form with file attachment in PHP


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •