1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

php attach file & email help needed

Discussion in 'Programming' started by nals, Jul 22, 2009.

  1. #1
    Can some please check this code & help me: I need to allow file type only (.jpg,.pdf,.doc,docx,gif )

    <?php
    // Read POST request params into global vars
    $to      = 'myemail@mydomain.com';
    $email   = $email;
    $subject = "New Attachment from Online Form";
    $message = $_POST['message'];
    
    	
    // Obtain file upload vars
    $fileatt      = $_FILES['fileatt']['tmp_name'];
    $fileatt_type = $_FILES['fileatt']['type'];
    $fileatt_name = $_FILES['fileatt']['name'];
    
    $headers = "From:" .$email;
    
    if (is_uploaded_file($fileatt)) {
      // Read the file to be attached ('rb' = read binary)
      $file = fopen($fileatt,'rb');
      $data = fread($file,filesize($fileatt));
      fclose($file);
    
      // 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" .
                 $message . "\n\n";
    
      // Base64 encode the file data
      $data = chunk_split(base64_encode($data));
    
      // Add file attachment to the message
      $message .= "--{$mime_boundary}\n" .
                  "Content-Type: {$fileatt_type};\n" .
                  " name=\"{$fileatt_name}\"\n" .
                  //"Content-Disposition: attachment;\n" .
                  //" filename=\"{$fileatt_name}\"\n" .
                  "Content-Transfer-Encoding: base64\n\n" .
                  $data . "\n\n" .
                  "--{$mime_boundary}--\n";
    }
    
    // Send the message
    $ok = @mail($to, $subject, $message, $headers);
    if ($ok) {
      echo "<p>Your Attachment submitted Sucessfuly - It will be reviewed soon</p>";
    } else {
      echo "<p>Mail could not be sent. Sorry!</p>";
    }
    ?>
    PHP:
    Thanks
     
    nals, Jul 22, 2009 IP
  2. nals

    nals Peon

    Messages:
    168
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Someone Please help me
     
    nals, Jul 23, 2009 IP
  3. nadad

    nadad Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    if your program works with txt attachments but not with pdf ..etc
    try it with stripslashes:
    $data = chunk_split(base64_encode(stripslashes($data)));
     
    nadad, Aug 5, 2009 IP
  4. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #4
    Hello Nals, try using the file type to check:

    
    <?php
    // Read POST request params into global vars
    $to      = 'myemail@mydomain.com';
    $email   = $email;
    $subject = "New Attachment from Online Form";
    $message = $_POST['message'];
    
       
    // Obtain file upload vars
    $fileatt      = $_FILES['fileatt']['tmp_name'];
    $fileatt_type = $_FILES['fileatt']['type'];
    $fileatt_name = $_FILES['fileatt']['name'];
    
    $headers = "From:" .$email;
    
    /*
    Allowed filetypes
    */
    
    $allowedFileTypes = array('image/jpg','image/gif','application/msword','application/pdf','application/vnd.openxmlformats');
    
    if (is_uploaded_file($fileatt) && in_array($fileatt,$allowedFileTypes)) {
      // Read the file to be attached ('rb' = read binary)
      $file = fopen($fileatt,'rb');
      $data = fread($file,filesize($fileatt));
      fclose($file);
    
      // 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" .
                 $message . "\n\n";
    
      // Base64 encode the file data
      $data = chunk_split(base64_encode($data));
    
      // Add file attachment to the message
      $message .= "--{$mime_boundary}\n" .
                  "Content-Type: {$fileatt_type};\n" .
                  " name=\"{$fileatt_name}\"\n" .
                  //"Content-Disposition: attachment;\n" .
                  //" filename=\"{$fileatt_name}\"\n" .
                  "Content-Transfer-Encoding: base64\n\n" .
                  $data . "\n\n" .
                  "--{$mime_boundary}--\n";
    }
    
    // Send the message
    $ok = @mail($to, $subject, $message, $headers);
    if ($ok) {
      echo "<p>Your Attachment submitted Successfully - It will be reviewed soon</p>";
    } else {
      echo "<p>Mail could not be sent. Sorry!</p>";
    }
    ?>
    
    PHP:
     
    ThePHPMaster, Aug 5, 2009 IP