SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast earph's Avatar
    Join Date
    May 2004
    Location
    Austin
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Formmail w/Attachment Not Working!

    Here's my code...... does anyone see any reason why my attachment isn't working?
    PHP Code:
    <?php
       
    // Get data from form
       
    $name $_POST['name'];
       
    $phone $_POST['phone'];
       
    $email $_POST['email'];
       
    $question $_POST['question']; 

       
    // Set mail recipient and subject
       
    $to "matt@earph.com"
       
    $subject "WINDOW SITE QUESTION";

       
    // Get image file from the form
       
    $fileatt      $_FILES['attachPhoto']['tmp_name'];
       
    $fileatt_type $_FILES['attachPhoto']['type'];
       
    $fileatt_name $_FILES['attachPhoto']['name'];

       
    // Set standard header
       
    $headers "From: $name\n" .
                  
    "MIME-Version: 1.0\n";

       if (
    is_uploaded_file($fileatt))
       {
          
    // Read the file to be attached ('rb' = read binary)
          
    $file fopen($fileatt'rb');
          
    $data fread($filefilesize($fileatt));
          
    fclose($fileatt);

          
    // Generate a boundary string
          
    $semi_rand md5(time());
          
    $mime_boundary "--MIME-boundary {$semi_rand} --";
          
          
    // Add headers for a file attachment
          
    $headers .= "Content-Type: multipart/mixed:\n\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/html; charset=\"iso-8859\"\n" .
                     
    "Content-Transfer-Encoding: 7bit\n\n";

          
    // Place form data into message
          
    $message .= printMessage($name$phone$email$question);

          
    // 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" .
                      
    " filename = \"{$fileatt_name}\"\n" .
                      
    "Content-Transfer-Encoding: base64\n\n" .
                      
    $data "\n\n" .
                      
    "$mime_boundary\n";
       }
       else
       {
          
    $headers .= "Content-Type: text/html\n\n";
          
    $message printMessage($name$phone$email$question);
       }

       
    mail($to$subject$message$headers);
       
    header("Location: thanks.asp");


       
    #############################
       ######### Functions #########
       #############################

       
    function printMessage($name$phone$email$question)
       
    // Preconditions:  None
       // Postconditions: Prints out data from the form formatted in HTML
       
    {
          
    $messageOut "<h2>Window Site Question</h2>" .
                        
    "<b>Name:</b> $name<br>" .
                        
    "<b>Phone:</b> $phone<br>" .
                        
    "<b>Email:</b> $email<br>" .
                        
    "<b>Question:</b> $question<br><br>";

          return 
    $messageOut;
       } 
    ?>

  2. #2
    SitePoint Enthusiast mjlivelyjr's Avatar
    Join Date
    Dec 2003
    Location
    Post Falls, ID, US
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yah, the part of the header where you specify content type and boundary is borked.

    First off, the content type header should look like this:
    PHP Code:
    $header .= "Content-Type: multipart/mixed;\n" .
                   
    " boundary="==Multipart_Boundary_xxc75885"; 
    Note, you need to only include one line break after the content type and you need to use a semi-colon...not a colon. Also, you don't NEED to have the boundary on a seperate line, this is done for readability purposes. If you don't include it on the same line you need to make sure you use one or more spaces as the first part of the string so e-mail clients no that it's a header continuation.

    Now, I would also recommend you not use any dashes (-) in the boundary. This runs the risk of confusing your client. Also, if you want to speed things up you don't really have to generate a random string for every e-mail you generate. Just so long as you can come up with 1 string that you are pretty sure won't ever appear in an e-mail, then you can just hardcode the string to a variable, it'll cut down a bit on your processing time as you won't have to come up with a new random string every time. In either case, try to stay away from dashes.

    Lastly, when you use the boundary in the e-mail address, ot only must it appear on it's own line but it HAS to be preceded by 2 hyphens. So the appropriate string would be "--{$mime_boundery}"

    I went ahead and reposted your code with my mods.

    PHP Code:
    <?php
       
    // Get data from form
       
    $name $_POST['name'];
       
    $phone $_POST['phone'];
       
    $email $_POST['email'];
       
    $question $_POST['question'];

       
    // Set mail recipient and subject
       
    $to "matt@earph.com";
       
    $subject "WINDOW SITE QUESTION";

       
    // Get image file from the form
       
    $fileatt      $_FILES['attachPhoto']['tmp_name'];
       
    $fileatt_type $_FILES['attachPhoto']['type'];
       
    $fileatt_name $_FILES['attachPhoto']['name'];

       
    // Set standard header
       
    $headers "From: $name\n" .
                  
    "MIME-Version: 1.0\n";

       if (
    is_uploaded_file($fileatt))
       {
          
    // Read the file to be attached ('rb' = read binary)
          
    $file fopen($fileatt'rb');
          
    $data fread($filefilesize($fileatt));
          
    fclose($fileatt);

          
    // Generate a boundary string
          //You might be getting some problems using the character '-'
          
    $semi_rand md5(time());
          
    $mime_boundary "==MIME_boundary_{$semi_rand}";
          
          
    // Add headers for a file attachment

          //these are also bad headers, you can never include a double linebreak withint the headers.
          // Also, the colon needs to be a semi-colon
          
    $headers .= "Content-Type: multipart/mixed;\n" .
                      
    "boundary=\"$mime_boundary\"\n\n";

          
    // Add a multipart boundary above the plain message
          
    $message "This is a multi-part message in MIME format.\n\n" .
                     
    //lastly, when the boundary is actually used IN the message you have to add 2 dashes (-) in front of it
                     
    "--$mime_boundary\n" .
                     
    "Content-Type: text/html; charset=\"iso-8859\"\n" .
                     
    "Content-Transfer-Encoding: 7bit\n\n";

          
    // Place form data into message
          
    $message .= printMessage($name$phone$email$question);

          
    // 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" .
                      
    " filename = \"{$fileatt_name}\"\n" .
                      
    "Content-Transfer-Encoding: base64\n\n" .
                      
    $data "\n\n" .
                      
    "--$mime_boundary\n";
       }
       else
       {
          
    $headers .= "Content-Type: text/html\n\n";
          
    $message printMessage($name$phone$email$question);
       }

       
    mail($to$subject$message$headers);
       
    header("Location: thanks.asp");


       
    #############################
       ######### Functions #########
       #############################

       
    function printMessage($name$phone$email$question)
       
    // Preconditions:  None
       // Postconditions: Prints out data from the form formatted in HTML
       
    {
          
    $messageOut "<h2>Window Site Question</h2>" .
                        
    "<b>Name:</b> $name<br>" .
                        
    "<b>Phone:</b> $phone<br>" .
                        
    "<b>Email:</b> $email<br>" .
                        
    "<b>Question:</b> $question<br><br>";

          return 
    $messageOut;
       }
    ?>
    Also, site point has a great tutorial on this subject: http://www.sitepoint.com/article/advanced-email-php

    I think the actual code for mixed messages starts on page 4.
    Mike Lively
    Digital Sandwich - MMM MMM Good

  3. #3
    SitePoint Enthusiast earph's Avatar
    Join Date
    May 2004
    Location
    Austin
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awesome..... thanks for the tips!


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
  •