Results 1 to 5 of 5

Thread: E-Mail with attachment

  1. #1
    Join Date
    Mar 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default E-Mail with attachment

    PHP Code:
    <?php
        
    //Set up email and attachment details
        
    $to "myemail@email.com";
        
    $subject "Application Form";
        
        
    // Store the file information to variables for easier access
        
    $tmp_name $_FILES['filename']['tmp_name'];
        
    $type $_FILES['filename']['type'];
        
    $name $_FILES['filename']['name'];
        
    $size $_FILES['filename']['size'];
        
    $job $_POST["job"];
        
    $salutation $_POST["salutation"];
        
    $fromename $_POST["fromname"];
        
    $address $_POST["address"];
        
    $phone $_POST["phone"];
        
    $fromemail $_POST["fromemail"];
        
    $about $_POST["about"];
      
        
    //Form contents
        
    $message "You have a job enquiry: $name \n
            Job Required: 
    $job \n
            Salutation: 
    $salutation \n
            From: 
    $fromename \n
            Address: 
    $address \n
            Telephone: 
    $phone \n
            E-mail: 
    $fromemail \n
            About yourself: 
    $about";
           
        
    //Read in the attachment
        
    if (file_exists($tmp_name)){
         if(
    is_uploaded_file($tmp_name)){
          
    $file fopen($tmp_name'rb');
          
    $data fread($filefilesize($tmp_name));
          
    fclose($file);
        
    //Encode it and split it into acceptable length lines
        
    $data chunk_split(base64_encode($data));
        }
        
        
    //Add the MIME content
        
    $semi_rand md5(time());
        
    $mime_boundary "==Multipart_Boundary_x".md5(mt_rand())."x";
        
    $headers "From: $fromemail\r\n".
             
    "MIME-Version: 1.0\r\n" .
             
    "Content-Type: multipart/mixed;\r\n" .
             
    "boundary=\"{$mime_boundary}\"";
        
        
    //Message header
        
    $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";     
        
         
    // Insert boundary to indicate we're starting the attachment
         // Specify the content type, file name and disposition as an attachment. 
         // Then add the file content and set another boundary to indicate that the end of the file has been reached
        
    $message .= "--{$mime_boundary}\n" .
             
    "Content-Type: {$type};\n" .
             
    " name=\"{$name}\"\n" .
             
    "Content-Disposition: attachment;\n" .
             
    " filename=\"{$name}\"\n" .
             
    "Content-Transfer-Encoding: base64\n\n" .
        
    $data "\n\n" .
             
    "--{$mime_boundary}--\n";
    }
    //Send the e-mail
    mail($to$subject$message$headers);         
        

    ?>
    Can anyone help me get this working? If I submit it without an attachment it works, as soon as I add an attachment it sends it but nothing appears in the sent e-mail. I think it is something to d with the uploading of the files…

    Any help greatly appreciated!
    Last edited by Snookerman; 11-02-2009 at 12:02 PM.

  2. #2
    Join Date
    Mar 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    C'mon someone must have an idea?

    Pretty please

  3. #3
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Post your HTML code for your form that's sending this data. The problem might be there, and might be very simple fix. THe php code looks correct.
    - Josh

  4. #4
    Join Date
    Oct 2009
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Execute this to check if mail() is enabled:

    Code:
    <?php
    
    if (function_exists('mail'))
    {
        echo "Its enabled, so its something else...";
    }
    else
    {
        echo "Function is disabled";
    }  
    
    ?>
    Also post your html, make sure your html form fields name tags all match to their correct $_POST variables.

  5. #5
    Join Date
    Nov 2008
    Posts
    58
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default

    Getting the MIME encoding right is not easy. It is better to use a library like Pear Mime encoding. See the page below for an example:
    How to create PHP email form with file attachment

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
  •