SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Attach file from server

    Hi guys..!!
    can anyone give me some idea how to attach a file from the server and send it as attachment through email??
    i have the following code:

    PHP Code:
    <?php
    // Read POST request params into global vars
    $to      $_POST['to'];
    $from    $_POST['from'];
    $subject $_POST['subject'];
    $message $_POST['message'];
    // Obtain file upload vars
    $fileatt      $_FILES['fileatt']['tmp_name'];
    $fileatt_type $_FILES['fileatt']['type'];
    $fileatt_name $_FILES['fileatt']['name'];
    $headers "From: $from";
    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>Mail sent! Yay PHP!</p>";
    } else {
      echo 
    "<p>Mail could not be sent. Sorry!</p>";
    }
    ?>
    but this code does not attach file from the server.
    please help me out.. thanks in advance..!!

  2. #2
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    6 Thread(s)
    Hi php_rockzz,

    I would recommend that you look at Swift Mailer. This is an email library that is simple to implement and performs email attachments much simpler than what your are trying to do in your script. There is a 10 part tutorial on DevShed where this part shows how to do dynamic attachments.

    Regards,
    Steve
    ictus==""


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
  •